C++中的condition_variable中在多线程中的使用

有这样的需求
一个线程需要等待另一个线程执行完毕之后它才会继续向下执行,这该如何实现?
condition_variable类中实现了wait方法

wait()

  • 可以接受两个参数,其中第一个参数是锁对象,第二个参数是lambda表达式
    其中的lambda表达式的返回值要是bool类型
  • 也可接受一个参数,仅仅接受锁对象
#include<iostream>
#include<mutex>
#include<list>
#include<thread>
using namespace std;




class Obj
{
private:
	list<int> myList;
	condition_variable var;
	mutex tex;
public:
	bool popFromList(int & comb)
	{
		if (!myList.empty()) {
			unique_lock<mutex> cur(tex);
			if (!myList.empty())
			{
				comb = myList.front();
				myList.pop_front();
				return true;
			}
		}
		return false;
	}
	void inToList()
	{
		
		for (int i = 0; i < 100; i++)
		{
			unique_lock<mutex> cur(tex);
			myList.push_back(i);
			cout << this_thread::get_id() << "正在向list中添加数据>; " << i << endl;
			var.notify_one();
		}
	}
	void outFromList()
	{
		while(true)
		{
			unique_lock<mutex> cur(tex);
			var.wait(cur, [this] {
				if (!myList.empty())
					return true;
				return false;
				});
			int comb = myList.front();
			myList.pop_front();
			cout << this_thread::get_id() << "正在取出数据>: " << comb << endl;
			
		}
	}
};


int main()
{
	Obj obj;
	thread thread1(&Obj::outFromList, &obj);
	thread thread2(&Obj::inToList, &obj);
	thread1.join();
	thread2.join();
	cout << "这里是主线程" << endl;

}

线程1执行到wait函数时,会执行lambda表达式

  • 返回值为false时,这个线程就会将锁打开,将该线程压入到栈中,执行下一个线程
    下一个线程执行完毕之后执行notify_one后就会返回到该线程,继续执行lambda表达式
    • 返回值为true时就尝试获得锁
      • 获得锁后继续执行下面的语句
      • 没有获得锁就继续卡在wait等待获取到锁
    • 返回值为false时,继续将锁打开,线程压入栈,执行下一个线程
  • 返回值为true时,线程继续执行

运行结果可能是一个线程执行完毕另一个线程再执行
这不就是顺序执行吗?
其实并不是这样的

notify_one()

唤醒wait()函数,当前所再的线程尝试获得锁
某个线程执行完notify_one函数后,会返回到另一个线程中的wait函数处,并将其"唤醒",让其继续执行,自己的线程和wait线程都尝试获得锁来进行下一步执行

不是顺序执行的解释

  • 当wait函数后面有很多语句要执行,但是再此之前wait所在的线程函数中就已经将锁进行释放了,那么notify_one的“唤醒”就不在生效,两个线程都尝试获得锁,但是wait所在的线程有很多语句要执行,耗时高,那么很有可能notify_one所在的线程就再次获得了锁,进行下一步操作。
  • 这样就不一定是顺序执行的宏观表现了
#include<iostream>
#include<mutex>
#include<list>
#include<thread>
using namespace std;




class Obj
{
private:
	list<int> myList;
	condition_variable var;
	mutex tex;
public:
	bool popFromList(int & comb)
	{
		if (!myList.empty()) {
			unique_lock<mutex> cur(tex);
			if (!myList.empty())
			{
				comb = myList.front();
				myList.pop_front();
				return true;
			}
		}
		return false;
	}
	void inToList()
	{
		
		for (int i = 0; i < 1000; i++)
		{
			cout << this_thread::get_id() << "正在向list中添加数据>; " << i << endl;
			unique_lock<mutex> cur(tex);
			myList.push_back(i);

			var.notify_one();
		}
		
	}

	void outFromList()
	{
		while(true)
		{
			unique_lock<mutex> cur(tex);
			var.wait(cur, [this] {
				if (!myList.empty())
					return true;
				return false;
				});
			int comb = myList.front();
			myList.pop_front();
			cur.unlock();
			cout << this_thread::get_id() << "正在取出数据>: " << comb << endl;
			chrono::seconds tim(2);
			this_thread::sleep_for(tim);
			cout <<this_thread::get_id() <<  "睡眠两秒后执行" << endl;
			
		}
	}
};


int main()
{
	Obj obj;
	thread thread1(&Obj::outFromList, &obj);
	thread thread2(&Obj::inToList, &obj);
	thread1.join();
	thread2.join();
	cout << "这里是主线程" << endl;

}

如图所示,在notify_one线程执行835次循环后,wait所在的线程才获得了锁,继续执行
在此之前的所有notify_one的唤醒操作都是无效的。
然后在notify_one线程执行完毕之后,wait再次获得了锁,继续向下执行
在这里插入图片描述
在这里插入图片描述

notify_all()

顾名思义,是“唤醒”所有的wait函数线程,但是并不是都一起运行,因为他们要进行锁的请求,仅仅只能由一把锁存在,只有获得锁并且lambda表达式返回结果为true的线程才能继续执行

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值