第六章 条件变量

12 篇文章 0 订阅
10 篇文章 0 订阅

前面的章节中,线程使用互斥变量同步访问普通资源,而本章将讨论另外一种同步问题。

案例代码:

#include<iostream>

#include<string>

#include<list>

#include<thread>

#include<functional>

#include<mutex>

  

using namespace std;

  

std::list<int> q;

  

std::mutex mu;

  

void function1()

{

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 function2()

{

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 data fomr t1:" << data << endl;

}

else

locker.unlock();

}

}

  

int main()

{

std::thread t1(function1);

std::thread t2(function2);

t1.join();

t2.join();

return 0;

}

  

从这段代码中可以看出来,list q 是一个被function1function2共享的序列,,因此在q.push_backfunction2中的q.pop_bak()之前都需要加锁。

但是在function2函数中,由于if… else…的使用,使得function2处于q.empty()的循环之中——如果不是进行pop操作,如果是则进行解锁操作,从而形成一种循环状态,导致程序运行低效。

  

  

解决方案一种是在else中的unlock()下面添加如下语句:

Std::this_thread::sleep_for(chrono::milliseconds(10));

  

但其不足之处在于等待的时间不容易控制。

另外一种解决方案则是本章的章节名称——条件变量。

  

Cond.wait(locker)可以将线程function2休眠,等到function1后notify_one()/all()通知后才向下进行。而使用wait中是locker的原因在于,使用互斥对象mu后,被线程function2锁住,一个被锁住的线程不会在被锁住的情况下休眠,使用wait(locker)之后,可以解锁互斥对象mu让其进行休眠,而后加锁。而q.pop_bak()后再解锁。(有问题)

  

由于线程function2中涉及到加/解锁,因此只能使用unique_lock<>,而不能使用lock_guard<>.

  

同时,线程function2可能被自己激活——称为伪激活,碰到伪激活,但不需要工作,这时需要使用lambda表达式

  

wait()中添加另外的参数:

Cond.wait(locker,[](){return !a.empty();});

  

清单如下

#include<iostream>

#include<string>

#include<list>

#include<thread>

#include<functional>

#include<mutex>

#include<condition_variable>

  

using namespace std;

  

std::list<int> q;

std::mutex mu;

std::condition_variable cond;

  

void function1()

{

int count = 10;

while (count > 0)

{

std::unique_lock<mutex>locker(mu);

  

q.push_back(count);

locker.unlock();

cond.notify_all(); //需要多个线程激活时使用notify_all(),如果有一个时,使用notify_one()即可

std::this_thread::sleep_for(chrono::seconds(1));

count--;

}

  

}

  

void function2()

{

int data = 0;

while (data != 1)

{

std::unique_lock<mutex> locker(mu);

cond.wait(locker, []() {return !q.empty(); }); //防止"伪激活"的出现

data = q.back();

q.pop_back();

locker.unlock();

cout << "t2 data fomr t1:" << data << endl;

  

}

}

  

int main()

{

std::thread t1(function1);

std::thread t2(function2);

t1.join();

t2.join();

return 0;

}

  

 

  

柏树 于 2016/8/17 星期三 8:43 修改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值