std::condition_variable is a synchronization primitive provided by the C++ Standard Library that allows threads to wait for changes to a shared variable while minimizing contention for a mutex.
The function notify_all() is a member function of std::condition_variable that unblocks all threads currently waiting on the condition_variable.
Here's an example code snippet that demonstrates the use of notify_all() in C++:
#include #include #include #include #include
std::mutex mtx; std::condition_variable cv;
std::vector data;
void worker_func() { std::unique_lock lck(mtx); cv.wait(lck, []{return data.size() > 0;}); // Do work with data... }
In this example, the worker thread waits on the condition_variable until there is data to process. When the main thread populates the data vector and calls notify_all(), the worker thread wakes up and processes the data.
The package library for std::condition_variable and notify_all() is the header in the C++ Standard Library.
C++ (Cpp) condition_variable::notify_all - 30 examples found. These are the top rated real world C++ (Cpp) examples of std::condition_variable::notify_all extracted from open source projects. You can rate examples to help us improve the quality of examples.