一、条件变量的引入

std::condition_variable 解决了死锁并且控制的资源的访问顺序二避免不必要的等待。当互斥操作不够用而引入的。比如,线程可能需要等待某个条件为真才能继续执行,而一个忙等待循环中可能会导致所有其他线程都无法进入临界区使得条件为真时,就会发生死锁。所以,condition_variable实例被创建出现主要就是用于唤醒等待线程从而避免死锁。std::condition_variable的 notify_one()用于唤醒一个线程;notify_all() 则是通知所有线程。

二、下面是一个引用的例子

假设想实现一个简单的消费者生产者模型,一个线程往队列中放入数据,一个线程往队列中取数据,取数据前需要判断一下队列中确实有数据,由于这个队列是线程间共享的,所以,需要使用互斥锁进行保护,一个线程在往队列添加数据的时候,另一个线程不能取,反之亦然。
一般的:

std::deque<int> q;
std::mutex mu; void function_1() {
int count = ;
while (count > ) {
std::unique_lock<std::mutex> locker(mu);
q.push_front(count);
std::cout << "t1 put a value : " << count << std::endl;
locker.unlock();
std::this_thread::sleep_for(std::chrono::seconds());
count--;
}
} void function_2() {
int data = ;
while (data != ) {
std::unique_lock<std::mutex> locker(mu);
if (!q.empty()) {
data = q.back();
q.pop_back();
locker.unlock();
std::cout << "t2 got a value from t1: " << data << std::endl;
}
else {
locker.unlock();
}
}
}
int main() {
std::thread t1(function_1);
std::thread t2(function_2);
t1.join();
t2.join();
return ;
}
std::this_thread::sleep_for(std::chrono::seconds(1));表示延时1s,所以这个生产的过程是很慢的;function_2函数是消费者,存在着一个while循环,只有在接收到表示结束的数据的时候,才会停止,每次循环内部,都是先加锁,判断队列不空,然后就取出一个数,最后解锁。所以说,在1s内,做了很多无用功!这样的话,单核CPU占用率会很高,可能达到100%。
// threadTest.cpp: 定义控制台应用程序的入口点。
#include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <string>
#include <fstream>
#include <deque>
#include <condition_variable> using namespace std; std::deque<int> q;
std::mutex mu;
std::condition_variable cond; void function_1()
{
int count = ;
while (count>)
{
std::unique_lock<mutex> locker(mu);
q.push_back(count);
locker.unlock();
cond.notify_all();
std::this_thread::sleep_for(chrono::seconds());
count--;
} } void funciton_2()
{
int data = ;
while (data != )
{
std::unique_lock<mutex> locker(mu);
cond.wait(locker, [](){return !q.empty(); }); // while(q.empty()) {cond.wait(locker); }// Unlock mu and wait to be notified 等同写法
        data = q.back(); 
q.pop_back();
locker.unlock();
cout << "t2 get a value form t1 " << data << endl; }
} int main() {
thread t1(function_1);
thread t2(funciton_2);
t1.join();
t2.join();
std::getchar(); return ; }

多线程一----多线程的应用

多线程二----简单线程管理

多线程三----数据竞争和互斥对象

多线程四----死锁和防止死锁

多线程五----unick_lock和once_flag

多线程六----条件变量

多线程七----线程间通信

c++11の条件变量的更多相关文章

  1. C++11 条件变量

    C++11中的条件变量提供了用户等待的同步机制,在同步队列的应用中有很大的便利. 简单同步队列代码如下(SimpleSyncQueue.h): #ifndef SIMPLESYNCQUEUE_H #d ...

  2. c++11 条件变量 生产者-消费者 并发线程

    http://baptiste-wicht.com/posts/2012/04/c11-concurrency-tutorial-advanced-locking-and-condition-vari ...

  3. C++11 中的线程、锁和条件变量

    转自:http://blog.jobbole.com/44409/ 线程 类std::thread代表一个可执行线程,使用时必须包含头文件<thread>.std::thread可以和普通 ...

  4. C++11并发——多线程条件变量std::condition_variable(四)

    https://www.jianshu.com/p/a31d4fb5594f https://blog.csdn.net/y396397735/article/details/81272752 htt ...

  5. c++11中的线程、锁和条件变量

    void func(int i, double d, const string& s) { cout << i << ", " << d ...

  6. C++11并行编程-条件变量(condition_variable)详细说明

    <condition_variable >头文件主要包含有类和函数相关的条件变量. 包括相关类 std::condition_variable和 std::condition_variab ...

  7. Windows:C++11并发编程-条件变量(condition_variable)详解

    <condition_variable >头文件主要包含了与条件变量相关的类和函数.相关的类包括 std::condition_variable和 std::condition_varia ...

  8. APUE学习笔记——11 线程同步、互斥锁、自旋锁、条件变量

    线程同步     同属于一个进程的不同线程是共享内存的,因而在执行过程中需要考虑数据的一致性.     假设:进程有一变量i=0,线程A执行i++,线程B执行i++,那么最终i的取值是多少呢?似乎一定 ...

  9. c++11多线程记录6:条件变量(condition variables)

    https://www.youtube.com/watch?v=13dFggo4t_I视频地址 实例1 考虑这样一个场景:存在一个全局队列deque,线程A向deque中推入数据(写),线程B从deq ...

随机推荐

  1. Chapter 5 Blood Type——15

    I hesitated, torn, but then the first bell sent me hurrying out the door — with a last glance confir ...

  2. Google SwipeRefreshLayout(Goolge官方下拉刷新控件)尝鲜

    前天Google官方终于出了Android刷新控件——SwipeRefreshLayout. 使用前先需要将android.support.v4.jar升级到19.1.升级后,可能会出现SDK版本与A ...

  3. linux添加C#运行环境

    linux是不带C#的运行环境的,同样的还有.NET. 有一个叫做Mono的很好用http://www.go-mono.com/,有给docker,而且有环境的选择,要注意. 安好后有给样例的程序,编 ...

  4. curl 错误总结

    证书域名与访问的域名不一致 问题:curl SSL: certificate subject name 'luffichen_server.tencent.com' does not match ta ...

  5. 如何正确使用Java泛型

    前言 Java 1.5之前是没有泛型的,以前从集合中读取每个对象都必须先进行转换,如果不小心存入集合中对象类型是错的,运行过程中转换处理会报错.有了泛型之后编译器会自动帮助转换,使程序更加安全,但是要 ...

  6. SDWebImage源码解析

    但凡经过几年移动开发经验的人去大公司面试,都会有公司问到,使用过哪些第三方,看过他们的源码嘛?而SDWebImage就是经常被面试官和应聘者的提到的.下面将讲述SDWebImage的源码解析以及实现原 ...

  7. Node.js 应用:Koa2 使用 JWT 进行鉴权

    前言 在前后端分离的开发中,通过 Restful API 进行数据交互时,如果没有对 API 进行保护,那么别人就可以很容易地获取并调用这些 API 进行操作.那么服务器端要如何进行鉴权呢? Json ...

  8. java-初识Properties

    1.通过代码了解一哈: package com.etc; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...

  9. ubuntu16.04 部署配置LVS主从

    实验环境---ubuntu16.04 四台机器:10.211.55.13—55.16 具体实验环境配置如下: 10.211.55.102  LVS_VIP 10.211.55.13  LVS_MAST ...

  10. Fragment 生命周期的详情

    Fragment每个生命周期方法的意义.作用(注意红色的不是生命周期方法):setUserVisibleHint():设置Fragment可见或者不可见时会调用此方法.在该方法里面可以通过调用getU ...