先列出这节课的例程代码.
#include <deque>
#include <functional>
#include <iostream>
#include <fstream>
#include <thread>
#include <string>
#include <mutex>
using namespace std;
std::deque<int> q;
std::mutex mu;
void function_1()
{
int count = 10;
while(count > 0)
{
std::unique_lock<mutex> locker(mu);
q.push_back(count);
locker.unlock();
std::this_thread::sleep_for(chrono::seconds(1));
count--;
}
}
void function_2()
{
int data = 0;
while(data !=1)
{
std::unique_lock<mutex> locker(mu);
if(!q.empty())
{
data = q.back();
q.pop_back();
locker.unlock();
cout << "t2 get a number :" << data << endl;
}
else
locker.unlock();
}
}
int main(int argc,char** argv)
{
thread t1(function_1);
thread t2(function_2);
t1.join();
t2.join();
return 0;
}
这段代码,主要是建立了两个子线程,一个子线程作为数据的生产者,另一个是数据的消费者.但是,这里在线程2,也就是function_2中,如果共享内存对象q中没有数据,则一直会去循环,不断的进行加锁,解锁,十分消耗计算机资源,所以可以添加如下延时代码,可以从某种程度上缓解这个问题,但是延时的时间大小却依旧不能很好的确定.
else
{
locker.unlock();
std::this_thread::sleep_for(chrono::milliseconds(10));
}
这个时候,就出现了这节课的救星-条件变量,闪亮登场的时候了.
#include <deque>
#include <functional>
#include <iostream>
#include <fstream>
#include <thread>
#include <string>
#include <mutex>
#include <condition_variable>
using namespace std;
std::deque<int> q;
std::mutex mu;
std::condition_variable cond;
void function_1()
{
int count = 10;
while(count > 0)
{
std::unique_lock<mutex> locker(mu);
q.push_back(count);
locker.unlock();
cond.notify_one(); //会激活一个等待这个条件的线程
std::this_thread::sleep_for(chrono::seconds(1));
count--;
}
}
void function_2()
{
int data = 0;
while(data !=1)
{
std::unique_lock<mutex> locker(mu);
cond.wait(locker,[](){return !q.empty();});//会将线程2休眠,直到线程1调用notify_one(),才会把它激活
//条件变量可以减少线程2许多无谓的循环,换句话就是说它可以控制线程间的访问顺序
//这里为什么会需要locker来作为wait()的参数呢?在wait()之前,线程2已经被mu锁住.
//一个线程不会在被锁住的时候休眠,在wait()实现中,会解锁互斥对象mu,然后将其休眠.
//休眠后,又要重新加锁,所以这里需要传入一个互斥对象.因为这里需要重复的加解锁,所以
//这里只能用unique_lock(),而不能使用lock_guard.
//此外,这里线程2可能会被自己激活,也就是所谓的伪激活.
//如果碰见伪激活,我们不希望线程2运行.所以我们需要为其添加第二个参数.
//当有多个像这样的等待线程存在时,我们可以将cond.notify_one()改为cond.notify_all()
cout << "deque size :" << q.size() << endl;
data = q.back();
q.pop_back();
locker.unlock();
cout << "t2 get a number :" << data << endl;
}
}
int main(int argc,char** argv)
{
thread t1(function_1);
thread t2(function_2);
t1.join();
t2.join();
return 0;
}