-1

I have a c application that connects to a TCP server as a client and the socket number that is returned from connect() I attempt to use in the main application and in 2 subthreads.

Is it ok to do this? For example my main application could be performing a socket write while the worker thread might be trying to read from the socket at the same time? It's just recently my application crashed and I'm worried this might be the cause

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

-1

Threads work simultaneously. When we try to modify any global parameter or shared resource, it needs to be locked using semaphores or mutex. In this case if the port is being used by any thread, the other thread will not be able to use it and might get an error of resource busy or may even lead to memory fault. Hence it is required to lock the resource ( port in this case ).

  • Sockets are full duplex. It will not cause either 'resource busy' or a memory fault. It can however cause an insoluble problem at the peer. And the question is about sockets, not ports. – user207421 Sep 12 '19 at 11:07
  • Ok, so the answer seems to be that I can use the created socket the way I am suggesting. Maybe it was not the cause of my crash. – Eoin O Connell Sep 12 '19 at 11:29
  • It is suggest you to try with mutex once. – Shivendra Pratap Kushwaha Sep 12 '19 at 11:34
  • It is suggested that you don't post guesswork here, or FUD about non-existent memory faults, and that you don't post about ports when the question is about sockets. Any system resource such as an FD or socket is already protected by the kernel during a system call as an atomic operation, and doesn't require an extra muted in the application space merely to replicate what the kernel already does. – user207421 Sep 12 '19 at 12:12