readerwriterqueue
readerwriterqueue copied to clipboard
Select multiple queues
What is the best way to wait behind multiple queues and being notified when a message is coming from any of them?
Actually I want something like select (in linux sockets or go programming language for channels).
What I am doing now is iterating over all of them and use the non-blocking api try_dequeue
, but this solution is busy wait and not efficient.
Short answer: You can't with the current API.
Longer answer: This could be done a few ways; perhaps the simplest is to use a semaphore, but this still requires iterating the queues on wakeup. (By sheer coincidence, this happens to exactly match the internal design of my BlockingConcurrentQueue -- enqueueing from multiple producers goes into separate internal sub-queues, and dequeueing waits on a shared semaphore.)
The best way to do it, as you say, is to use an OS-level primitive (select, poll, WaitForMultipleObjects) where the pool of objects waited upon is tied to corresponding queues. This is doable, but rather platform-specific.
Thanks