Sleep until a certain condition is met [c++]

359 Views Asked by At

so I have a simple networking code, where it checks if an atomic priority queue, which can be pushed from several different threads, is empty or not. If it is not empty, it will take that value and do some operation with it.

    for (;;) {
        REQUEST currentRequest; // REQUEST is an enum
        
        bool isEmpty = m_RequestsQueue.GetFrontAndPop(currentRequest);

        if (!isEmpty)
        {
            // some unrelated networking operations
            ...
        }            
    }

The problem with the above code is that my infinite loop is CPU intensive. I can not afford to add stuff like std::this_thread::sleep_for(std::chrono::milliseconds(20)); to reduce the CPU load, as some REQUEST that might be added to the queue might need to be handled right away.

How can one add a "sleep until condition is met" in a thread? I don't mind using boost if needed.

In the above example, the condition would be that the size of my array is greater than 0 for example

Thanks!

0

There are 0 best solutions below