I am planning to use boost::lockfree::queue for my multi threaded application. A boost example illustrates lockfree queue consumption like this:
boost::atomic<bool> done (false);
void consumer(void)
{
int value;
while (!done) {
while (queue.pop(value))
++consumer_count;
}
while (queue.pop(value))
++consumer_count;
}
my question is this part:
while (!done) {
//do something
}
I usually used to use condition variable
for such cases but the simplicity of the above code snippet is far more tempting than going through the complexity of condition variables.
Although the consumer
will have its own thread(s), it loops almost for the entire duration of program. I worry more because there are many times that the //do something
part is not invoked(queue is empty) and a lot of CPU time, which can be given to other threads, is wasted by this thread. Am I right? Is THIS a common practice?
I need someone to tell me I am wrong and I shouldn't worry about this for so&so reasons. or suggest me a better approach.
thanks