条件变量是另外一种用于等待的同步机制,它能阻塞一个或多个线程,直到收到另一个线程发出的通知或者超时才会唤醒当前阻塞的线程。
条件变量需要和互斥量配合使用。
C++11 头文件 <condition_variable > 中包含了与条件变量想关的类和函数:
- 类
- std::condition_variable
- std::condition_variable_any
- 枚举
- cv_status
- 函数
- notify_all_at_thread_exit
condition_variable
当 std::condition_variable 对象的某个等待函数被调用的时候,它使用 std::unique_lockstd::mutex 来锁住当前线程。当前线程会一直被阻塞,直到另外一个线程在相同的 std::condition_variable 对象上调用了通知函数来唤醒当前线程。
std::condition_variable 对象通常使用 std::unique_lockstd::mutex 来等待,如果需要使用另外的 lockable 类型,可以使用后面激昂会介绍的 std::condition_variable_any 类。
-
成员函数
// 构造析构 (constructor) // 只提供了默认构造函数 (destructor) // 等待函数 wait wait_for wait_until // 通知函数 notify_one // 唤醒一个因该条件变量阻塞的线程 notify_all // 唤醒所有因该条件变量阻塞的线程
- wait_* 方法需要传入一个 std::unique_lock 对象。当线程中条件变量调用 wait 方法之后当前线程将会被阻塞,同时会调用 std::unique_lock::unlock 方法释放锁。当线程获得通知被唤醒时自动调用 std::unique_lock::lock 上锁。
- wait_* 方法另一个版本需要追加一个参数,该参数是一个返回值为布尔类型的可调用对象,当它返回 false 时调用 wait 会阻塞线程,返回 true 时调用 wait 不会阻塞线程。相当于:
while (!callableObject()) wait(lck);
- notify_one 通知一个被条件变量阻塞的进程,如果没有因为该条件变量进入等待的线程则什么也不做,如果有一个则唤醒该线程,如果有多个线程在等待,那么不能保证具体唤醒哪一个线程。
-
应用实例
/*************************************************************** * (constructor) * wait * notify_all * ************************************************************/ std::mutex mtx01; std::condition_variable cv01; bool runFlag = false; void func01(int index) { std::unique_lock <std::mutex> ulck(mtx01); if (!runFlag) cv01.wait(ulck); std::cout << index << ": thread id " << std::this_thread::get_id() << std::endl; } void runThreads() { std::lock_guard<std::mutex> glck(mtx01); runFlag = true; cv01.notify_all(); } std::vector<std::thread> thVec(2); for (int i = 0; i < 2; ++i) { thVec[i] = std::move(std::thread(func01, i + 1)); } std::this_thread::sleep_for(std::chrono::milliseconds(500)); runThreads(); for (auto& th : thVec) { th.join(); } /* // --------------------------------------------------------- // output // --------------------------------------------------------- 2: thread id 16868 1: thread id 28712 */
condition_variable_any
std::condition_variable_any 与 std::condition_variable 类似,只不过 std::condition_variable_any 的 wait 函数可以接受任何 lockable 参数,而 std::condition_variable 只能接受 std::unique_lockstd::mutex 类型的参数。 除此以外两者几乎完全一样。
cv_status
cv_status::no_timeout // wait_for 或者 wait_until 没有超时,即在规定的时间段内线程收到了通知
cv_status::timeout // wait_for 或者 wait_until 超时
notify_all_at_thread_exit
当条件变量调用 notify_all_at_thread_exit 函数的线程退出时,所有在该条件变量上等待的线程都会收到通知。
notify_all_at_thread_exit 的函数原型如下:
void notify_all_at_thread_exit (condition_variable& cond, unique_lock<mutex> lck);
应用实例:
std::mutex mtx01;
std::condition_variable cv01;
bool runFlag = false;
void func01(int index)
{
std::unique_lock <std::mutex> ulck(mtx01);
if (!runFlag) cv01.wait(ulck);
std::cout << index << ": thread id " << std::this_thread::get_id() << std::endl;
}
void runThreads()
{
std::unique_lock<std::mutex> ulck(mtx01);
std::notify_all_at_thread_exit(cv01, std::move(ulck));
}
int main()
{
std::vector<std::thread> thVec(10);
for (int i = 0; i < 10; ++i)
{
thVec[i] = std::move(std::thread(func01, i + 1));
}
std::this_thread::sleep_for(std::chrono::milliseconds(500));
std::thread(runThreads).detach();
for (auto& th : thVec)
{
th.join();
}
}
/*
// ---------------------------------------------------------
// output
// ---------------------------------------------------------
8: thread id 36480
7: thread id 10476
4: thread id 42556
2: thread id 37532
10: thread id 14644
6: thread id 11304
3: thread id 26508
9: thread id 21116
5: thread id 44584
1: thread id 24276
*/