Program to Illustrate the Use of Condition Variable

C++
// C++ Program to illustrate the use of Condition Variables
#include <condition_variable>
#include <iostream>
#include <mutex>
#include <thread>

using namespace std;

// mutex to block threads
mutex mtx;
condition_variable cv;

// function to avoid spurios wakeup
bool data_ready = false;

// producer function working as sender
void producer()
{
    // Simulate data production
    this_thread::sleep_for(chrono::seconds(2));

    // lock release
    lock_guard<mutex> lock(mtx);

    // variable to avoid spurious wakeup
    data_ready = true;

    // logging notification to console
    cout << "Data Produced!" << endl;

    // notify consumer when done
    cv.notify_one();
}

// consumer that will consume what producer has produced
// working as reciever
void consumer()
{
    // locking
    unique_lock<mutex> lock(mtx);

    // waiting
    cv.wait(lock, [] { return data_ready; });

    cout << "Data consumed!" << endl;
}

// drive code
int main()
{
    thread consumer_thread(consumer);
    thread producer_thread(producer);

    consumer_thread.join();
    producer_thread.join();

        return 0;
}


Output

Data Produced!
Data consumed!

In this program, the consumer thread uses the condition variable cv to wait until data_ready is set to true while the producer thread sleeps for two seconds to mimic data generation.

Condition Variables in C++ Multithreading

In C++, the condition variable is a synchronization primitive that is used to notify the other threads in a multithreading environment that the shared resource is free to access. It is defined as the std::condition_variable class inside the <condition_variable> header file.

Similar Reads

Need for Condition Variable in C++

Condition variable is especially needed in cases where one thread has to wait for another thread execution to continue the work. For example, the producer-consumer relationship, sender-receiver relationship, etc....

Syntax of std::condition_variable

The syntax to declare a condition variable is simple:...

Condition Variable Methods

The std::condition_variable methods contain some member methods to provide the basic functionalities. Some of these are:...

Example: Program to Illustrate the Use of Condition Variable

C++ // C++ Program to illustrate the use of Condition Variables #include #include #include #include using namespace std; // mutex to block threads mutex mtx; condition_variable cv; // function to avoid spurios wakeup bool data_ready = false; // producer function working as sender void producer() { // Simulate data production this_thread::sleep_for(chrono::seconds(2)); // lock release lock_guard lock(mtx); // variable to avoid spurious wakeup data_ready = true; // logging notification to console cout << "Data Produced!" << endl; // notify consumer when done cv.notify_one(); } // consumer that will consume what producer has produced // working as reciever void consumer() { // locking unique_lock lock(mtx); // waiting cv.wait(lock, [] { return data_ready; }); cout << "Data consumed!" << endl; } // drive code int main() { thread consumer_thread(consumer); thread producer_thread(producer); consumer_thread.join(); producer_thread.join(); return 0; }...

Errors Associated with C++ Condition Variable

The condition variable is prone to the following errors:...

Advantages of Condition Variable

The following are the major advantages of using condition variables in our C++ program:...

Conclusion

In conclusion, condition variables are an effective tool for assuring safe access to shared data, lowering contention, and establishing effective thread synchronisation in multi-threaded C++ programmes. They are commonly used with the mutex to provide an efficient synchronization technique....