从windows角度来说,condition_variable类似event。

阻塞等待出发,不过condition_variable可以批量出发。

代码如下:

// 1111111.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <thread>
#include <mutex>
#include <condition_variable> std::mutex gmtx;
std::condition_variable gcv;
bool gready = false; void do_print_id(int id)
{
std::unique_lock<std::mutex> lock(gmtx);
while (!gready)
gcv.wait(lock);
std::cout << "thread " << id << std::endl;
} void go()
{
std::unique_lock<std::mutex> lock(gmtx);
gready = true;
gcv.notify_all();
} int _tmain(int argc, _TCHAR* argv[])
{
std::thread thread[];
for (int i = ; i < ; ++i)
thread[i] = std::thread(do_print_id, i); std::cout << "start..." << std::endl;
//触发激活
go(); //等待线程结束执行
for (auto & th : thread)
th.join(); return ;
}

显示如下:

start...
thread 7
thread 3
thread 2
thread 9
thread 6
thread 5
thread 1
thread 0
thread 4
thread 8
请按任意键继续. . .

#pragma once
// 参考http://www.cnblogs.com/qicosmos/archive/2013/05/30/3107975.html
#include <thread>
#include <condition_variable>
#include <mutex>
#include <list>
#include <iostream>

using namespace std;

template<typename T>
class SimpleSyncQueue {
public:
void PutWithLock(const T& x)
{
std::lock_guard<std::mutex> locker(m_mutex);
m_queue.emplace_back(x);
m_notEmpty.notify_one();
}
template<typename U>
void PutWithLock(const T&& x)
{
std::lock_guard<std::mutex> locker(m_mutex);
m_queue.emplace_back(std::forward<U>(x));
m_notEmpty.notify_one();
}
void PopWithLock(T& x)
{
std::unique_lock<std::mutex> locker(m_mutex);
while (m_queue.empty())
m_notEmpty.wait(locker);
x = m_queue.front();
m_queue.pop_front();
}
private:
std::list<T> m_queue;
std::mutex m_mutex;
std::condition_variable m_notEmpty;
};

c++11多线程学习笔记之三 condition_variable使用的更多相关文章

  1. c++11多线程学习笔记之四 生产消费者

    #ifndef MY_QUEUE_H__ #define MY_QUEUE_H__ #include<list> #include<mutex> #include<thr ...

  2. c++11多线程学习笔记之一 thread基础使用

    没啥好讲的  c++11  thread类的基本使用 #include "stdafx.h" #include <iostream> #include <thre ...

  3. Java多线程学习笔记之三内存屏障与Java内存模型

    基本内存屏障 处理器支持那种内存重排序,就会提供能够禁止相应内存重排序的的指令,这些指令就被成为基本内存屏障:StroeLoad屏障.StroeLoad屏障.LoadLoad屏障.LoadStore屏 ...

  4. c++11多线程学习笔记之二 mutex使用

    // 1111111.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include ...

  5. JAVA多线程学习笔记(1)

    JAVA多线程学习笔记(1) 由于笔者使用markdown格式书写,后续copy到blog可能存在格式不美观的问题,本文的.mk文件已经上传到个人的github,会进行同步更新.github传送门 一 ...

  6. java进阶-多线程学习笔记

    多线程学习笔记 1.什么是线程 操作系统中 打开一个程序就是一个进程 一个进程可以创建多个线程 现在系统中 系统调度的最小单元是线程 2.多线程有什么用? 发挥多核CPU的优势 如果使用多线程 将计算 ...

  7. java多线程学习笔记——详细

    一.线程类  1.新建状态(New):新创建了一个线程对象.        2.就绪状态(Runnable):线程对象创建后,其他线程调用了该对象的start()方法.该状态的线程位于可运行线程池中, ...

  8. 多线程学习笔记九之ThreadLocal

    目录 多线程学习笔记九之ThreadLocal 简介 类结构 源码分析 ThreadLocalMap set(T value) get() remove() 为什么ThreadLocalMap的键是W ...

  9. Java多线程学习笔记(一)——多线程实现和安全问题

    1. 线程.进程.多线程: 进程是正在执行的程序,线程是进程中的代码执行,多线程就是在一个进程中有多个线程同时执行不同的任务,就像QQ,既可以开视频,又可以同时打字聊天. 2.线程的特点: 1.运行任 ...

随机推荐

  1. oracle报ora-12519错误

    具体信息如下: ora-12519 tns:no appropriate service handler found the connection descriptor used by the cli ...

  2. 哈希学习(2)—— Hashing图像检索资源

    CVPR14 图像检索papers——图像检索 1.  Triangulation embedding and democratic aggregation for imagesearch (Oral ...

  3. 比较完整的URL验证

    转自:http://wuchaorang.2008.blog.163.com/blog/static/4889185220135279223253/ function IsURL(str_url){v ...

  4. docker容器中搭建kafka集群环境

    Kafka集群管理.状态保存是通过zookeeper实现,所以先要搭建zookeeper集群 zookeeper集群搭建 一.软件环境: zookeeper集群需要超过半数的的node存活才能对外服务 ...

  5. 一个NPOI导出到excel文件的范例记录

    '使用NPOI写入新创建的excel文件,导出文件: Private Sub Sub_WriteXls() Dim XlsBook As XSSFWorkbook Dim XlsSheet As XS ...

  6. VBA 定义能返回数组公式的自定义函数

    返回一个变量大小结果数组的方法 此方法返回基于一个参数范围的值的数组.结果数组的大小具体取决于参数数组中的元素数量波动.例如对于假定您要创建一个范围中的每个值乘以 100 的函数.下面的自定义函数接受 ...

  7. css 学习网址

    http://www.divcss5.com/ http://www.divcss5.com/css3/  css3手册 http://www.divcss5.com/shouce/ css手册 ht ...

  8. UI5-文档-4.31-Routing and Navigation

    到目前为止,我们已经把所有的应用程序内容放在一个页面上.随着我们添加越来越多的特性,我们希望将内容拆分并将其放在不同的页面上. 在这一步中,我们将使用SAPUI5导航特性加载并显示一个单独的详细信息页 ...

  9. Mybatis知识(1)

    1.#{}和${}的区别是什么? #{}是预编译处理,${}是字符串替换. Mybatis在处理#{}时,会将sql中的#{}替换为?号,调用PreparedStatement的set方法来赋值: M ...

  10. Windows7 64bit+python3.6环境下安装OpenCV3.3

    安装opencv3.3 打开windows的Python扩展包网址 根据自己的系统选择下载,这里我选择的是 通过pip3安装该whl文件,使用如下命令  pip3 install 该whl的绝对路径 ...