
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
C++ Library - <condition_variable>
The <condition_variable> in C++ is a synchronization primitive that allows threads to wait for certain conditions to be met before proceeding. It is useful in multi-threading applications, where one or more threads need to coordinate their actions based on shared data. By using condition variables, we can manage concurrent tasks, ensuring that threads either wait or proceed depending on specific conditions.
It is used in conjunction with std::mutex and is generally used with std::unique_lock. The unique_lock is passed to the condition_variable to release the lock when the thread is waiting, allowing the other threads to modify the shared data.
Including <condition_variable> Header
To include the <condition_variable> header in your C++ program, you can use the following syntax.
#include <condition_variable>
Functions of <condition_variable> Header
Below is list of all functions from <condition_variable> header.
Sr.No. | Functions & Description |
---|---|
1 |
notify_one
It notifies one waiting thread. |
2 |
notify_all
It notifies all waiting threads. |
3 |
wait
It blocks the current thread until the condition variable is awakened. |
4 |
wait_for
It blocks the current thread until the condition variable is awakened or after the specified timeout duration. |
Thread Waiting
In the following example, we are going to show how a thread can wait for a condition to be true.
#include <iostream> #include <thread> #include <condition_variable> #include <mutex> std::mutex a; std::condition_variable b; bool ready = false; void x() { std::unique_lock < std::mutex > lock(a); std::cout << "Waiting For Signal..." << std::endl; b.wait(lock, [] { return ready; }); std::cout << "Signal Received" << std::endl; } int main() { std::thread y(x); std::this_thread::sleep_for(std::chrono::seconds(3)); { std::lock_guard < std::mutex > lock(a); ready = true; } b.notify_one(); y.join(); return 0; }
Output
Following is the output of the above code −
Waiting For Signal... Signal Received