readerwriterqueue icon indicating copy to clipboard operation
readerwriterqueue copied to clipboard

Select multiple queues

Open thegreathir opened this issue 3 years ago • 2 comments

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.

thegreathir avatar Jun 03 '21 16:06 thegreathir

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.

cameron314 avatar Jun 06 '21 14:06 cameron314

Thanks

thegreathir avatar Jun 11 '21 22:06 thegreathir