#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. 五十五.ansible概述、ansible基础 、ad-hoc、批量配置管理

    1.环境准备 (自动化工具,批量操作) 6台 2cpu,1.5G以上内存,20G硬盘,1网卡 1.1 基础环境准备 1)启动6台虚拟机,ansible.sh   2)真机配置yum仓库 ]# tar ...

  2. Java进阶知识20 Spring的代理模式

    本文知识点(目录): 1.概念  2.代理模式      2.1.静态代理      2.2.动态代理      2.3.Cglib子类代理 1.概念 1.工厂模式  2. 单例模式 代理(Proxy ...

  3. ECMAScript 5.0 基础语法(下)“稍微重点一点点”

    接上篇 七.常用内置对象(复杂数据类型)(重点) (1)数组Array 创建:例  var colors = ['red','blue','green']       #推荐这样,因为简单粗暴 或:v ...

  4. .net Core使用 MongoDB

    1.安装mogodb windows版本下载地址:https://www.mongodb.com/download-center/v2/community 查看mongod.conf文件,找到绑定的I ...

  5. openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库

    openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库¶ https://www.osgeo.cn/openpyxl/index.html

  6. linux系统rwx(421)、777权限详解

    摘要 linux的常见权限,mark一下 常用的linux文件权限如下: 444 r--r--r-- 600 rw------- 644 rw-r--r-- 666 rw-rw-rw- 700 rwx ...

  7. centos7的网络配置参考

    <鸟哥的Linux私房菜>中的相关介绍和配置:http://linux.vbird.org/linux_basic/0610hardware.php 修改链接(connection)的名字 ...

  8. Maven:element '******' cannot have character [children]

    此错误是由于XML文件的解析不正确造成的,因为在一个/某些标签之间存在奇怪和隐藏的字符. 这些字符可能来自网络上的复制粘贴.要解决此问题,请删除标签>标记定义之间的所有空格和换行符,然后将它们放 ...

  9. Pwnhub Fantastic Key-一点总结

    index.php <? php error_reporting(0); include 'config.php'; $id = $_POST['i'] ? waf($_POST['i']) : ...

  10. linux系统安装硬盘分区建议

      一.常见挂载点的情况说明一般来说,在linux系统中都有最少两个挂载点,分别是/ (根目录)及 swap(交换分区),其中,/ 是必须的: 详细内容见下文: 安装系统时选择creat custom ...