c++ 多线程 condition_variable

条件变量:条件为假时阻塞线程并释放锁;或无条件阻塞

只有被notify了才判断条件是否变为真就唤醒线程;或无条件唤醒

相关成员函数:wait, wait_for, notify_one, notify_all

wait函数示例:

#include<iostream>
#include<thread>
#include<mutex>
#include<atomic>
#include<condition_variable>
using namespace std;

mutex mu;
condition_variable con;
int global=0;

inline bool global_not_zero(){
    return global!=0;
}

void fun(int n){
    for(int i=0;i<n;i++){
        unique_lock<mutex> lck(mu);
        con.wait(lck, global_not_zero); //global为零,阻塞
        printf("thread %d\n",global);
    	global=0;
    }
}

int main()
{
    cout<<"hello world!"<<endl;
    thread t1(fun, 10);
    for(int i=0;i<10;i++){
    	while(global_not_zero()) this_thread::yield(); //global不为零,让出时间片
	    global = i+1;
        unique_lock<mutex> my_lck(mu); //如果没有这一行会导致死循环,想想为什么(如果在条件变量即将要阻塞子线程前global=i+1并notify了,那么子线程就错过了被notify然后被阻塞就永远等不到notify了因为主线程进入了while循环而且条件永为真,所以notify前要要加锁保证子线程已经被阻塞)
    	con.notify_all();
	printf("main %d\n",global);
    }
    t1.join();
    return 0;
}

wait_for函数示例:

#include<iostream>
#include<thread>
#include<mutex>
#include<condition_variable>
#include<chrono>
using namespace std;

condition_variable cv;
int global_v;

void read(){
    cin>>global_v;
    cv.notify_one();  //通知一个线程
}

int main()
{
    cout<<"Please enter a number: "<<endl;
    thread t(read);
    //如果没有锁和条件变量,主线程在子线程输入之前就可能打印global_v,因为多线程并行
    mutex mu;
    unique_lock<mutex> lck(mu);
    while(cv.wait_for(lck, chrono::seconds(5))==cv_status::timeout){
        //通知或超时都会解锁,超时解锁返回timeout,未超时解锁返回no_timeout
        cout<<"5 secs passed..."<<endl;
    }
    
    cout<<"you entered "<<global_v<<endl;
    
    t.join();
    return 0;
}

参考:C++多线程详解(全网最全) - 知乎 (zhihu.com)

多线程中如何安全的使用std::cout - 简书 (jianshu.com) 

条件变量的notify_all()是否应该加锁 - 知乎 (zhihu.com) 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值