Search code examples
cmultithreadingsocketsnetwork-programmingposix-select

sharing socket descriptor between multiple threads


Just a quick question. I have a network C application (TCP server) which has multiple worker threads(Pthreads). Each worker thread uses a select system call and each thread has the listening socket descriptor added to its select set. So effectively, each worker thread listens for incoming connections, and only one thread at a time succeeds in accepting the particular connection and that connection's socket descriptor is added to the respective thread's select set.

My question is that each thread has its own select set; is it possible that I can send or receive data to a client whose socket descriptor is in another worker thread's select set? In other words, can I use a socket descriptor from any worker thread's select set to perform I/O from any thread I want?


Solution

  • You can share sockets and file descriptors between different threads. That's what most servers do.