资料地址

http://en.cppreference.com/w/cpp/thread/condition_variable/wait_until

http://www.cnblogs.com/haippy/p/3252041.html

 // asdasdwa.cpp: 定义控制台应用程序的入口点。
// #include "stdafx.h" #include <mutex> // std::mutex, std::unique_lock
#include <iostream>
#include <string>
#include <atomic>
#include <condition_variable>
#include <thread>
#include <chrono>
#include <vector> #include <windows.h>
using namespace std::chrono_literals; /*
作 者: itdef 欢迎转帖 请保持文本完整并注明出处
技术博客 http://www.cnblogs.com/itdef/
技术交流群 群号码:324164944
欢迎c c++ windows驱动爱好者 服务器程序员沟通交流
部分老代码存放地点 http://www.oschina.net/code/list_by_user?id=614253
*/ std::condition_variable cv;
std::mutex mtx;
std::vector<std::string> messageVec; std::atomic<int> i; void SingalFunc() {
std::lock_guard<std::mutex> lock(mtx);
messageVec.push_back("1234567890test.hello world!");
std::cout << "push" << std::endl;
cv.notify_one();
} void WaitTimeOutFunc1()
{
while()
{
std::unique_lock<std::mutex> lck(mtx);
auto now = std::chrono::system_clock::now();
if (cv.wait_until(lck, now + 2s, []() {return !messageVec.empty(); })) {
std::cerr << "Thread " << " finished waiting. " << std::endl;
messageVec.pop_back();
}
else {
std::cerr << "Thread " << " timed out." << std::endl;
return;
}
}
} void WaitTimeOutFunc()
{
while()
{
std::unique_lock<std::mutex> lck(mtx);
std::cv_status status = std::cv_status::timeout;
auto now = std::chrono::system_clock::now();
while (messageVec.empty()) {
status = cv.wait_until(lck, now + 1s);
if (status == std::cv_status::timeout) {
std::cout << i << " timeout" << std::endl;
return;
}
} i.fetch_add();
std::cout << i << " no_timeout" << std::endl;
// std::cout << messageVec.back() << std::endl;
messageVec.pop_back();
}
return;
} int main()
{
std::thread t[];
for (int i = ; i < ; i++) {
SingalFunc();
}
for (int i = ; i <; i++) {
t[i] = std::thread(WaitTimeOutFunc);
} //for (int i = 0; i < 5000; i++) {
// SingalFunc();
//} for (int i = ; i < ; i++) {
t[i].join();
} return ;
}

c++11 并发 条件变量 超时等待的代码练习的更多相关文章

  1. Go语言协程并发---条件变量

    package main import ( "fmt" "sync" "time" ) func main() { //要监听的变量 bit ...

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

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

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

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

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

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

  5. 转 C++11 并发指南std::condition_variable详解

    之前看过,但是一直没有怎么用就忘了,转一篇别人的文字记录下来 本文将介绍 C++11 标准中 <condition_variable> 头文件里面的类和相关函数. <conditio ...

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

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

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

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

  8. C++11 并发编程库

    C++11 并发编程 C++11 新标准中引入了几个头文件来支持多线程编程,他们分别是: <atomic>:该头文主要声明了两个类, std::atomic 和 std::atomic_f ...

  9. Linux多线程编程详细解析----条件变量 pthread_cond_t

    Linux操作系统下的多线程编程详细解析----条件变量 1.初始化条件变量pthread_cond_init #include <pthread.h> int pthread_cond_ ...

随机推荐

  1. Frame animation

    [Frame animation] An animation defined in XML that shows a sequence of images in order (like a film) ...

  2. Handling Event

    [Handling Event] 1.React events are named using camelCase 2.You must call preventDefault explicitly ...

  3. leetcode题库解答源码(python3)

    下面和大家分享本人在leetcode上已经ace的题目源码(python3): 本人会持续更新!- class Leetcode_Solution(object): def twoSum_1(self ...

  4. docker容器和镜像

    这篇文章希望能够帮助读者深入理解Docker的命令,还有容器(container)和镜像(image)之间的区别,并深入探讨容器和运行中的容器之间的区别. 当我对Docker技术还是一知半解的时候,我 ...

  5. Java自学之路(小白向)

    首先学习一门语言无非就受教与自学两种方式,本博文针对自学(穷逼没钱报班,大学又不是计算机专业,只能自学啊) 开个玩笑,其实有很多人是适合自学的,下面介绍. 一.要充满自信我自己就可以学好(够面试用的入 ...

  6. 2019.3.15 关于IE

    1. .clearfix {zoom:1} zoom:1   是ie浏览器专有属性  它可以设置或检索对象缩放比例  处理ie的hasLayout属性  清除浮动  清除margin的重叠

  7. blockchain good article

    https://medium.com/programmers-blockchain/create-simple-blockchain-java-tutorial-from-scratch-6eeed3 ...

  8. [z]spring boot gradle build

    I had the same problem. I believe it is caused by the JRE that gradle is configured to use rather th ...

  9. c# 运行大运算程序主窗体卡掉的解决

    写了一个运算过滤大文本的程序, 其中方法里边使用了多线程,并行线程等方法.  但主窗体控件直接使用此方法时,页面卡顿.所以主线程被堵塞. 代码如下, splitfile 这个方法运行时页面卡顿,阻塞了 ...

  10. Linux系统安装过程

    一.安装VMware® Workstation 14 Pro,有一些安装时会出现virtual XT的错误,这需要重启电脑进入BIOS,开户虚拟加速开关:有些win8/10会出现虚拟网卡安装失败的情况 ...