A conditional variable is used to block and object until it is “notified” to makeup.
To use conditional variables, we need to use below header:
#include <condition_variable> // std::condition_variable
Below are the Wait functions
Below are the Notify functions
Suppose you have a bank account with 0 balance. In such case, if a withdraw and a deposit request comes at same time, withdraw request will need to wait till the deposit can happen.
In such cases, the withdraw can wait for some time and after deposit, it can notify the withdraw thread to withdraw money.
Now let us understand with the help of an example:
Example:
In the below example, I have created 2 methods. deposit() and withdraw().
In the main() function, we called withdraw first, as the initial balance is 0, it will wait till the deposit occurs.
Then we call deposit() function, it will deposit the money.
Then we shall notify the waiting thread.
Then the withdraw() will be called, and amount will be withdrawn.
#include <iostream> // std::cout #include <chrono> // std::chrono::milliseconds #include <thread> // std::thread #include <mutex> // std::timed_mutex #include <condition_variable> // std::condition_variable // for more tutorials on C, C++, DS visit www.ProDeveloperTutorial.com using namespace std; std::condition_variable cv; std::mutex mtx; int balance = 0; void deposit(int money) { std::lock_guard<mutex> lg(mtx); balance = balance + money; cout<<"The current balance is "<<balance<<endl; cv.notify_one(); } void withdraw(int money) { std::unique_lock <mutex> ul(mtx); cv.wait(ul, [] { return (balance != 0) ? true : false; }); if ( balance >= money) { balance -= money; cout<<"The amount has been withdrawn"<<endl; } else { cout<<"The current balance is less than the amount"<<endl; } cout<<"The current balance is"<<balance<<endl; } int main () { std::thread t1 (withdraw, 400); std::thread t2 (deposit, 600); t1.join(); t2.join(); return 0; }
Output:
The current balance is 600 The amount has been withdrawn The current balance is 200
List Of Tutorials available in this website: