Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 423
Abdullah Avatar asked Oct 28 '25 08:10

Abdullah


1 Answers

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

like image 150
MByD Avatar answered Oct 31 '25 00:10

MByD



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!