从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. 19.XPath选择器

    1.extract():提取数据 2./text()     :获取节点内容文本 3./@href   :获取节点href属性 4. @         :获取属性名称 需要注意问题: 用定义的规则那 ...

  2. Swagger从入门到精通

    https://legacy.gitbook.com/book/huangwenchao/swagger/details 如何编写基于OpenAPI规范的API文档 [TOC] 前言 编写目的 本文介 ...

  3. 基于二进制RPC协议法的轻量级远程调用框架 ---- Hessian

    使用Java创建Hessian服务有四个步骤: 1.创建Java接口作为公共API                             (client和server端 创建一个相同的借口) 2.使 ...

  4. as3 AIR 添加或删除ApplicationDirectory目录下文件

    AIR的文件目录静态类型有五种: File.userDirectory //指向用户文件夹 File.documentsDirectory //指向用户文档文件夹 File.desktopDirect ...

  5. 前端-CSS-2-选择器

    基本选择器 首先来说一下,什么是选择器.在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式, 某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素之后 ...

  6. HQL中的Like查询需要注意的地方

    public List getOrgan(String organCode, String organName) {    String hsql;    List list; if (organCo ...

  7. 视频采集,存成avi

    视频采集,存成aviunit Unit1; interface uses  Windows, Messages, SysUtils, Classes, Graphics, Controls, Form ...

  8. scala -- 传名参数

    object Test{ def main(args: Array[String]): Unit = { def test(code : => Unit){// 传名参数 不计算函数值,而是把函 ...

  9. python格式化输出(转)

    在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作%. 模板 格式化字符串时,Python使用一个字符串作为模板.模 ...

  10. centos7 端口转发

    firewall-cmd --add-masquerade   firewall-cmd --add-forward-port=port=3001:proto=tcp:toaddr=172.17.18 ...