I'm using Boost's lockfree queue for a producer-consumer queue. I'd like each consumer thread to block when the queue is empty, waking up either when there's more data in the queue, or any producer terminates. But Boost doesn't seem to offer a blocking pop
, only an immediate returning pop
.
How should consumers wait until data is available?
You're looking to do an interesting operation: you're looking to do a blocking operation on a lockfree queue, which is kinda the opposite of what you have a lockfree queue for.
Use a normal blocking queue using a mutex and a condition variable. It's easy, and its a more standard way to do it.
You actually pay a performance penalty for lockfree in many cases, because you're guaranteeing that the queue does not hold any locks, even in the worst cases.
This question covers many of the pros and cons of both approaches.