In this recipe, you'll learn about another C++ tool that's available in the standard library that allows multiple threads to communicate. We'll be using std::condition_variable and std::mutex to develop a producer-consumer program.
Learning inter-thread communication with condition variables
How to do it...
The program in this recipe will use std::mutex to protect the queue from concurrent access and std::condition_variable to notify the consumer that an item has been pushed to the queue. Let's get started:
- Open a new file called conditionVariable.cpp and type the following code into it:
#include <iostream>
#include <queue>
#include <condition_variable>
#include <...