Search code examples
c++multithreadingboostlock-free

Block a lock free queue using std::conditional_variable


I have a multi-threaded single producer single consumer process in which I use a lock free queue (boost::lock_free::spsc_queue).

Right now I have two while loops running (tight and without any wait) and as soon as anything is available on the queue, it is processed. However, I don't want my consumer side to be working full blown (while(true)) even though there is nothing to process. Hence, I am looking for a behavior similar to below.

bool running = true;

void producer() {
    while(running) {
        if (\*something to process*\) 
        {
            \*process data and push to the queue*\  
        } 
    }
}

void consumer() {
    while (running) {
        if (!queue.empty()) {
            \* pop and process data *\
        } else {
            \* wait till queue is not empty *\
        }
    }
}

To give more context the queue has market data, so, at times, push might not happen for a long time on the producer side, and that is when I want my consumer thread to wait. Hence, I am looking at some kind of event handling for this.

I have been looking at conditional_variable but can't seem to adapt it to the current context, since I do not have any kind of locking in place.

If anybody has any ideas to implement such behavior (using STL or boost), I would highly appreciate it. Thanks.


Solution

  • I'd recommend using ::concurrent_bounded_queue. It is able to sleep on a OS event waiting for new items or freed space (when hitting specified capacity limit). It uses fine-grained user-level synchronization and is lock-free on the hot path (if 'lock-free' is a brand that sells).