This article will analyze the “wait()” function with the condition variable C++.
What is the Condition Variable in C ++?
The condition variable in C++ is an object which manages the thread to acquire and release the resources. It blocks the calling threads until another thread is notified, hence it resumes the blocked thread. It works as a synchronization process between threads.
std::condition_variable::wait in C++
The std::condition_variable::wait first identifies the protection of shared resources of variables, applies the “wait()” function, and releases the thread if the condition satisfies, At the end, it notifies the condition_variable.
Example: Use std::condition_variable::wait
To implement the “std::condition_variable::wait” in C++, first, include the “<iostream>”, “<thread>”, “<mutex>”, and “<condition_variable>” libraries:
#include <thread>
#include <mutex>
#include <condition_variable>
Here:
- “<iostream>” library is used to take input/output and print them on the terminal.
- “<thread>” library is mainly used to create and manage the threads in a program.
- “<mutex>” class ensures individual access to shared resources by offering synchronization communication between threads.
- “<condition_variable>” library in C++ provides thread coordination, such as shared variables and synchronization with mutex lock based on conditions.
Next, we define a condition_variable as a “student”. Then, declare the mutex variable as “m_lock”, and the integer type variable “number” initialize with the “0”:
std::mutex m_lock;
int number = 0;
After that, we have defined a void “total_marks()” function which takes one integer parameter named “marks”. At first, this function applies lock on mutex “m_lock” by “t2” (thread). Next, append the “number” variable with “marks” and use the “cout” to display the value. Then, specify the “notify_one()” function to notify the condition variable:
{
std::unique_lock ul(m_lock);
number += marks;
std::cout<<"Total Marks of a Student:"<<marks<<"\n";
student.notify_one();
}
Now, we define a second function named “obtain_marks()” which also takes integer type parameters as “marks”. Then, apply the unique_lock on mutex by the “t1” thread. Next, invoke a “wait()” function to wait until the “if” condition is satisfied. The “if” condition will check if the value of the “marks” variable is less and equal to the number value then print the values of the “marks” variable. Otherwise, the “else” condition will be executed:
{
std::unique_lock ul(m_lock);
student.wait(ul,[] {return (number!=0) ? true : false;});
if(number>=marks)
{
number -= marks;
std::cout<<"Obtained Marks of a Student:"<<marks<<"\n";
}
else std::cout<<"Low number \n";
std::cout<<"Remaining marks is:" <<number;
}
The “main()” function creates the threads “t1” and “t2”. Then, pass the values as 800 to the function “obtain_marks”, and 1000 to the “total_marks” function, respectively. After this, call the “t1.join()” and “t2. join()”. The “join()” function is basically used to block the thread until the first thread completes the process according to the correct functionality:
{
std::thread t1(obtain_marks,800);
std::thread t2(total_marks,1000);
t1.join();
t2.join();
return 0;
}
The provided image indicates the output of the above-stated code:
That’s it! You have learned about the std::condition_variable::wait in C++.
Conclusion
A condition variable in C++ is used as an object which takes a “mutex” object to block the thread until it is notified to resume again. It used some member functions, such as the “wait()” and “notify_one()”. The mutex object simply acquires a lock on threads according to system specifications. In this article, We have demonstrated the std::condition_variable::wait in C++.