std::condition_variable

std::condition_variable,是C++11提供的条件变量,可用于同时阻塞一个线程或多个线程。一般的,生产者线程利用支持std::mutex的std::lock_guard/std::unique_lock修改共享变量后,并通知condition_variable。消费者线程获取同一个std::mutex(std::unique_lock所持有),并调用std::condition_variable的wait, wait_for, or wait_until。wait操作会释放互斥量,同时挂起该线程。当条件变量收到通知、超时到期或发生虚假唤醒时,线程被唤醒,互斥量也被原子地重新获取。需要注意的是,如果是虚假唤醒,线程应该检查条件并继续等待,以保证业务的正确性。

std::condition_variable::wait()

unconditional (1) void wait( std::unique_lockstd::mutex& lock );
predicate (2) template< class Predicate >
void wait( std::unique_lock< std::mutex >& lock, Predicate stop_waiting );

wait() 有两个重载,第二个算是一个语法糖,可用于在等待特定条件变为真时忽略虚假唤醒。相当于:

while (!stop_waiting()) {wait(lock);
}

注意:lock必须在wait()前调用,可以用来守护访问stop_waiting()。wait(lock)返回后会重新获取该lock。

示例:

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <condition_variable>std::mutex mtx;
std::condition_variable cv;
std::string data;
bool ready = false;
bool processed = false;void worker_thread()
{// Wait until main() sends datastd::unique_lock lock(mtx);cv.wait(lock, []{return ready;});// after the wait, we own the lock.std::cout << "Worker thread is processing data\n";data += " after processing";// Send data back to main()processed = true;std::cout << "Worker thread signals data processing completed\n";// Manual unlocking is done before notifying, to avoid waking up// the waiting thread only to block again (see notify_one for details)lock.unlock();cv.notify_one();
}int main()
{std::thread worker(worker_thread);data = "Example data";// send data to the worker thread{std::lock_guard lock(mtx);ready = true;std::cout << "main() signals data ready for processing\n";}cv.notify_one();// wait for the worker{std::unique_lock lock(mtx);cv.wait(lock, []{return processed;});}std::cout << "Back in main(), data = " << data << '\n';worker.join();
}

输出:

main() signals data ready for processing
Worker thread is processing data
Worker thread signals data processing completed
Back in main(), data = Example data after processing

std::condition_variable相关推荐

  1. C++ 多线程:条件变量 std::condition_variable

    文章目录 描述 使用 描述 头文件<condition_variable> 定义 class condition_variable; 简介 之前我们也已经介绍过了C++多线程中互斥变量存在 ...

  2. C++11中std::condition_variable的使用

    <condition_variable>是C++标准程序库中的一个头文件,定义了C++11标准中的一些用于并发编程时表示条件变量的类与方法等. 条件变量是并发程序设计中的一种控制结构.多个 ...

  3. 【多线程】1.条件变量--std::condition_variable

    条件变量允许我们通过通知进而实现线程同步. 因此,您可以实现发送方/接收方或生产者/消费者之类的工作流. 在这样的工作流程中,接收者正在等待发送者的通知.如果接收者收到通知,它将继续工作. 1. st ...

  4. 【转】C++11 并发指南五(std::condition_variable 详解)

    http://www.cnblogs.com/haippy/p/3252041.html 前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三 ...

  5. C++ std::condition_variable wait() wait_for() 区别

    一.std::condition_variable 是条件变量. wait() 当 std::condition_variable 对象的某个 wait 函数被调用的时候,它使用 std::uniqu ...

  6. C++11 并发指南五(std::condition_variable 详解)

    前面三讲<C++11 并发指南二(std::thread 详解)>,<C++11 并发指南三(std::mutex 详解)>分别介绍了 std::thread,std::mut ...

  7. Multi-thread--C++11中std::condition_variable的使用

    <condition_variable>是C++标准程序库中的一个头文件,定义了C++11标准中的一些用于并发编程时表示条件变量的类与方法等. 条件变量是并发程序设计中的一种控制结构.多个 ...

  8. 关于std::thread以及std::condition_variable的一些细节备忘

    也算是看过不少多线程相关的资料了,但是一直对于其中的一些细节没有太好的把握,比如std::thread线程真正开始运行的时机,比如join.detch等真正的作用. 跟着<Cplusplus C ...

  9. C++并发中的条件变量 std::condition_variable

    简介 这个操作相当于操作系统中的Wait & Signal原语,程序中的线程根据实际情况,将自己阻塞或者唤醒其他阻塞的线程. 个人认为,条件变量的作用在于控制线程的阻塞和唤醒,这需要和锁进行相 ...

  10. C++ std::condition_variable::notify_one()与notify_all()

    std::condition_variable的成员函数notify_one()与notify_all()是用来唤醒阻塞在wait()的线程.假如有多个线程调用condition_variable:: ...

最新文章

  1. 武器化道路越走越远的无人机
  2. NAT双出口的热备份
  3. Android系统编译时集成三方APK
  4. [云炬创业基础笔记] 第四章测试12
  5. Java关键字(53个关键字)
  6. 6月30日后支付宝还能正常提现吗?因为银行直连要停止了
  7. jq动态获取input的值传给html,jquery 保证html()拿到的html字符串包含input的value值
  8. 正则表达式总结之查找
  9. 世上最简单的mysql_最简单易懂的mysql安装教程
  10. 2019年5月数据库流行度排行:老骥伏枥与青壮图强
  11. Windows核心编程学习九:利用内核对象进行线程同步
  12. Delphi Web前端开发教程(1):基于TMS WEB Core框架
  13. 关于JEECG低代码框架使用笔记
  14. viper4android hifi,体验HiFi级音质 蝰蛇音效进阶使用教程
  15. proteus——rs-232双机通讯
  16. 网络安全人员必考的几本证书
  17. java多人聊天_java编程实现多人聊天室功能
  18. 《如何高效学习》读后感
  19. ls算法java实现_Java API之算法 | 学步园
  20. Android上传蒲公英平台脚本

热门文章

  1. python:pygame制作童年的游戏打砖块
  2. 深富策略:股市中优先股融资的优缺点有哪些
  3. 视频网站带宽计算方法(求拍砖)
  4. python decimal_Python中的decimal模块执行精确的浮点运算
  5. 对计算机病毒防治最科学的方法,常见的计算机病毒防范方法有哪些
  6. android实现自动取消订单,android – 使用RemoteView自动取消自定义通知
  7. 天池数据--幸福感数据分析(一)
  8. Java动态数据生成PDF文档及下载
  9. 11.10 VLAN实验总结
  10. FPGA实现Sobel边缘检测学习心得