#include <mutex>
#include <condition_variable>
#include <chrono>
#include <thread>
#include <glog/logging.h> class Event {
public:
Event();
~Event(); bool wait(std::chrono::milliseconds millisec);
void notify(); private:
std::mutex m_lock;
std::condition_variable m_cond;
}; Event::Event() {
} Event::~Event() {
} bool Event::wait(std::chrono::milliseconds millisec) {
LOG(INFO)<< "before lock";
std::unique_lock<std::mutex> l(m_lock);
auto cv = m_cond.wait_for(l, millisec);
if (cv == std::cv_status::no_timeout) {
return true;
}
return false;
} void Event::notify() {
m_cond.notify_all();
} class App {
public:
App();
~App(); bool start();
static void threadRun(void * p);
void run();
void stop();
void postExit(); private:
Event m_event;
std::mutex m_lock;
std::thread * m_thread;
}; App::App() {
m_thread = NULL;
} App::~App() {
m_thread = NULL;
} bool App::start() {
std::unique_lock<std::mutex> l(m_lock);
if (m_thread != NULL) {
LOG(INFO)<<"thread running";
return false;
}
m_thread = new std::thread(threadRun, this);
if (m_thread == NULL) {
LOG(INFO)<<"create thread failed";
return false;
}
LOG(INFO)<< "create thread success";
return true;
} void App::threadRun(void *p) {
App * pThis = (App*) p;
pThis->run();
} void App::run() {
while (!this->m_event.wait(std::chrono::milliseconds(1002))) {
LOG(INFO)<< "sleep";
}
} void App::postExit() {
delete this->m_thread;
this->m_thread = NULL;
} void App::stop() {
std::unique_lock<std::mutex> l(m_lock);
this->m_event.notify();
this->m_thread->join();
postExit();
} int main(int ac, char**av) {
google::InitGoogleLogging(av[0]); FLAGS_alsologtostderr = true;
FLAGS_logtostderr = true; LOG(INFO)<< "app started"; std::unique_ptr<App> p(new App);
if (!p->start()) {
LOG(INFO)<< "start failed";
return 1;
}
for (auto i = 0; i < 3; i++) {
std::this_thread::sleep_for(std::chrono::seconds(2));
LOG(INFO)<<"main thread sleep";
}
p->stop(); LOG(INFO)<< "main thread done";
return 0;
}

C++11 thread condition_variable mutex 综合使用的更多相关文章

  1. c++11 thread的学习

    http://www.cnblogs.com/wxquare/p/6736202.html 还没开始 留个链接 使用c++11 thread支持实现  一个生产者消费者模型 下面是一个生产者消费者问题 ...

  2. 通过c++11的condition_variable实现的有最大缓存限制的队列

    之前曾写过一个通过C++11的condition_variable实现的有最大缓存限制的队列,底层使用std::queue来实现,如果想要提升性能的话,可以考虑改用固定的长度环形数组.环形数组实现如下 ...

  3. C++11 Thread多线程的学习心得与问题

    C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...

  4. 漫谈C++11 Thread库之原子操作

    我在之前一篇博文<漫谈C++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程 ...

  5. c+11 std::condition_variable and mutex

    multiple threads synchronization primitive: 多线程同步语义 多线程的同步语义是多线程编程的核心,线程之间通过同步语义进行通信,实现并发.C++ JAVA 中 ...

  6. C++11 并发之std::thread std::mutex

    https://www.cnblogs.com/whlook/p/6573659.html (https://www.cnblogs.com/lidabo/p/7852033.html) C++:线程 ...

  7. C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)

    前面八章介绍了 C++11 并发编程的基础(抱歉哈,第五章-第八章还在草稿中),本文将综合运用 C++11 中的新的基础设施(主要是多线程.锁.条件变量)来阐述一个经典问题——生产者消费者模型,并给出 ...

  8. C++11中的mutex, lock,condition variable实现分析

    本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...

  9. 漫谈c++11 Thread库之使写多线程程序

    c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...

随机推荐

  1. 003转载----C#打开网页

    作者:微wx笑 来源:CSDN 原文:https://blog.csdn.net/testcs_dn/article/details/42246969 版权声明:本文为博主原创文章,转载请附上博文链接 ...

  2. Math.cbrt() Math.sqrt() Math.pow()

    Math.pow() 能实现 Math.cbrt() 和 Math.sqrt() 的功能,但并不完全相同. 1. Math.pow()和Math.cbrt()的区别 function isCube(m ...

  3. ZROI NOI2019集训汇总

    Day1 T1:已改但咕了(下午就补,没力气写了...) T2:未改 T3:未改 Day2 T1:在这里 T2:未改 T3:在这里 Day3 T1:在这里 T2:博客写在了本地,结果被数据清空了... ...

  4. maven+SSM+junit+jetty+log4j2环境配置的最佳实践

    思路大致是 jetty插件 -> junit -> SpringMVC -> Spring -> log4j2 -> Mybatis整合 pom中的依赖跟着思路一批一批的 ...

  5. Ubuntu 14.04 更换为阿里云源

    #备份sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak#编辑sudo vim /etc/apt/sources.list,清空后,加入以下 ...

  6. python 小游戏 “外星飞船入侵”

    Project Title 项目名称 “外星飞船入侵”, git地址:https://github.com/lilinyu861/Alien-Invasion 效果图: 参考: https://blo ...

  7. tar遇到error:"Error exit delayed from previous errors"的几种可能原因

    1.使用root用户解压压缩包 2.赋予权限,sudo chmod 777 xxx.tar.gz 参考: https://blog.csdn.net/iamwrr/article/details/49 ...

  8. 常用exporter下载

    1.node_exporter https://github.com/prometheus/node_exporter/releases/download/v0.18.1/node_exporter- ...

  9. python爬虫-爬坑之路

    背景简介 爬取外国的某两个网站的数据,网站都没有被墙,爬取三种数据. A: 爬取页面并存储到数据库 B: 爬取页面内的表格内数据并存储到数据库 C: 爬取页面,分析页面并将页面的所有数据分类存入数据库 ...

  10. Docker安装mysql5.6

    1.docker hub 上查找mysql镜像 2.在docker hub上(阿里云加速器)拉取mysql镜像到本地标签为5.6 3.使用mysql5.6创建容器(也叫运行镜像) 4.交互运行,进入m ...