Search code examples
c++multithreadingboostlock-freebusy-waiting

wont infinite loop waste cpu resource?


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


Solution

  • It is a very common practice for latency sensitive applications, i.e. applications for which the time spent for waking up a thread is not acceptable.

    Yes, in that case (it is called "spinning"), CPU time is wasted to check the boolean value. Spinlocks are implemented in a similar fashion, making them preferable in scenario where busy waiting is preferred.

    When the latency of the the producer-to-consumer path is not critical, you should prefer condition variables (or even explicit sleeping) to share the CPU with other thread/processes. And anyway, when latency is critical, you rarely want a lock-free container (that usually exposes a significant overhead to avoid locking)