Reputation: 177
When we want to design a generic Event Handling (I/O Demultiplexing) or reactor pattern model. The underlying system call we use is "select" or "poll". But both of the system call is not thread safe on common set of FD's. Can not be used in Multithreading environment.
What would be the better approach to handle events with multiple threads across multiple I/O's.
The 1 way I can see is, main thread recv's any events and push into a shared queue of thread pool. But worker thread can not send the data over the I/O causes synchronization problems. Also has memory overflow disadvantage.
All possible suggestions are welcome. Thanks in Advance.
Upvotes: 2
Views: 3857
Reputation: 7356
Most Unixes provide more scalable alternatives to select/poll that can be used in multithreaded environments:
But getting it right in a multithreaded environment can be tricky, so you might want to look at existing abstraction layers like boost.asio
On the other hand, boost.asio does introduce some non-negligible overhead - I have collected some information about that and an alternative abstraction for epoll/kqueue at http://nginetd.cmeerw.org
Upvotes: 1
Reputation: 68738
One way is to have one input thread, one output thread, several worker threads, and two blocking queues.
Input thread parses input messages and places them in queue 1. Worker threads all wait on queue 1, process a message and places any output they have in queue 2. Output thread waits on queue 2 and serializes the output from it.
InputThread:
Loop:
M = ReadNextMessage
Q1.push(M)
AddOutput(O):
Q2.push(O)
WorkerThread:
Loop:
M = Q1.pop
ProcessMessage(M) using AddOutput as needed
OutputThread:
Loop:
O = Q2.pop
WriteOutput(O)
However I don't know what you mean that select isn't threadsafe ? All system calls are technically threadsafe
It's very old but still very interesting is an article called "The CY10K problem" by a guy called Kegel..
http://www.kegel.com/c10k.html
It discusses different ways to structure a "multi io" program on Linux and the pros and cons of each.
Upvotes: 0
Reputation: 848
Check out the "Producer - Consumer problem", this is a very good starting point for thread-safe problems. It can also be expanded to multiple producers and multiple consumers.
Upvotes: 0