什么是条件变量?
条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足。条件变量上的基本操作有:触发条件(当条件变为 true 时);等待条件,挂起线程直到其他线程触发条件。条件变量要和互斥量相联结,以避免出现条件竞争--一个线程预备等待一个条件变量,当它在真正进入等待之前,另一个线程恰好触发了该条件。
什么意思呢?不清楚没关系。看了例子就知道了:问题描述:假设有一个bool型全局变量 isTrue ,现有10个线程,线程流程如下:当isTrue为真时,doSomething;否则挂起线程,直到条件满足。那么,用thread和mutex如何实现这个功能呢?
#include <vector>
#include <iostream>
#include <thread>
#include <mutex>
#include <chrono>
using namespace std;
bool isTrue = false;
void doSomething()
{
cout << "this is : " << this_thread::get_id() << endl;
}
void thread_Func()
{
while (!isTrue)
this_thread::yield();
doSomething();
}
int main()
{
vector<thread> v;
v.reserve(10);
for (int i = 0; i < 10; i++)
{
v.emplace_back(thread_Func);
}
this_thread::sleep_for(chrono::seconds(2));
isTrue = true;
for (auto& t : v)
{
t.join();
}
return 1;
}
这段代码虽然能满足需求,但有一个大缺点,就是当条件为假时,子线程会不停的测试条件,这样会消耗系统资源。我们的思想是,当条件为假时,子线程挂起,直到条件为真时,才唤醒子线程。
nice,条件变量就是干这事的!
先来看看条件变量的介绍:
条件变量能够挂起调用线程,直到接到通知才会唤醒线程。它使用unique_lock<Mutex>来配合完成。下面是用条件变量实现需求:
#include <iostream>
#include <vector>
#include <thread>
#include <mutex>
#include <chrono>
#include <condition_variable>
using namespace std;
bool isTrue = false;
std::mutex mtx;
std::condition_variable cv;
void doSomething()
{
cout << "this is : " << this_thread::get_id() << endl;
}
void thread_Func()
{
unique_lock<mutex> loc(mtx);
while (!isTrue)
cv.wait(loc);
doSomething();
}
int main()
{
vector<thread> v;
v.reserve(10);
for (int i = 0; i < 10; i++)
{
v.emplace_back(thread_Func);
}
this_thread::sleep_for(chrono::seconds(2));
{
unique_lock<mutex> loc(mtx);
isTrue = true;
cv.notify_all();
}
for (auto& t : v)
{
t.join();
}
return 1;
}
我们发现,在条件变量cv的wait函数中,我们传入了一个lock参数,为什么要用锁呢?因为isTrue为临界变量,主线程中会“写”它,子线程中要“读”它,这样就产生了数据竞争,并且这个竞争很危险。
我们把while (!isTrue) cv.wait(loc)拆开:
1、条件判断
2、挂起线程
假如现在1执行完后,时间片轮转,该子线程暂停执行。而恰好在这时,主线程修改了条件,并调用了cv.notify_all()函数。这种情况下,该子线程是收不到通知的,因为它还没挂起。等下一次调度子线程时,子线程接着执行2将自己挂起。但现在主线程中的notify早已经调用过了,不会再调第二次了,所以该子线程永远也无法唤醒了。
为了解决上面的情况,就要使用某种同步手段来给线程加锁。而c++11的condition_variable选择了用unique_lock<Mutex>来配合完成这个功能。并且我们只需要加锁,条件变量在挂起线程时,会调用原子操作来解锁。
c++11还为我们提供了一个更方便的接口,连同条件判断,一起放在条件变量里。上面的线程函数可做如下修改:
void thread_Func()
{
unique_lock<mutex> loc(mtx);
cv.wait(loc, []() -> bool { return isTrue;} );
doSomething();
}