C++11 thread condition_variable mutex 综合使用
#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 综合使用的更多相关文章
- c++11 thread的学习
http://www.cnblogs.com/wxquare/p/6736202.html 还没开始 留个链接 使用c++11 thread支持实现 一个生产者消费者模型 下面是一个生产者消费者问题 ...
- 通过c++11的condition_variable实现的有最大缓存限制的队列
之前曾写过一个通过C++11的condition_variable实现的有最大缓存限制的队列,底层使用std::queue来实现,如果想要提升性能的话,可以考虑改用固定的长度环形数组.环形数组实现如下 ...
- C++11 Thread多线程的学习心得与问题
C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...
- 漫谈C++11 Thread库之原子操作
我在之前一篇博文<漫谈C++11 Thread库之使写多线程程序>中,着重介绍了<thread>头文件中的std::thread类以及其上的一些基本操作,至此我们动手写多线程程 ...
- c+11 std::condition_variable and mutex
multiple threads synchronization primitive: 多线程同步语义 多线程的同步语义是多线程编程的核心,线程之间通过同步语义进行通信,实现并发.C++ JAVA 中 ...
- C++11 并发之std::thread std::mutex
https://www.cnblogs.com/whlook/p/6573659.html (https://www.cnblogs.com/lidabo/p/7852033.html) C++:线程 ...
- C++11 并发指南九(综合运用: C++11 多线程下生产者消费者模型详解)
前面八章介绍了 C++11 并发编程的基础(抱歉哈,第五章-第八章还在草稿中),本文将综合运用 C++11 中的新的基础设施(主要是多线程.锁.条件变量)来阐述一个经典问题——生产者消费者模型,并给出 ...
- C++11中的mutex, lock,condition variable实现分析
本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...
- 漫谈c++11 Thread库之使写多线程程序
c++11中最重要的特性之一就是对多线程的支持了,然而<c++ primer>5th却没有这部分内容的介绍,着实人有点遗憾.在网上了解到了一些关于thread库的内容.这是几个比较不错的学 ...
随机推荐
- mouseover([[data],fn])
mouseover([[data],fn]) 概述 当鼠标指针位于元素上方时,会发生 mouseover 事件. 该事件大多数时候会与 mouseout 事件一起使用.直线电机选型 注释:与 mous ...
- java新建excel文件导出(HSSFWorkbook)
public ActionForward exportExcel(ActionMapping mapping, ActionForm form, HttpServletRequest request, ...
- 02_通过位置变量创建 Linux 系统账户及密码
#!/bin/bash#$1 是执行脚本的第一个参数,$2 是执行脚本的第二个参数useradd "$1"echo "$2" | passwd --stdin ...
- Visual Studio Code:以十六进制格式显示文件内容
造冰箱的大熊猫@cnblogs 2019/9/4 发现Visual Studio Code很好用,无论是作为源代码编辑器还是文本编辑器在Win平台下用的都很不错.但有时候需要以十六进制格式查看数据文件 ...
- 2019.7.9 校内测试 T2 极值问题
这一次是交流测试?边交流边测试(滑稽 极值问题 乍一看这是一道数学题,因为1e9的数据让我暴力的心退却. 数学又不好,不会化简式子嘞,咋办? 不怕,咱会打表找规律.(考场上真的是打表找出了规律,打表打 ...
- LVM问题-----Insufficient Free Extents for a Logical Volume
Linux LVM学习——Insufficient Free Extents for a Logical Volume 如下所示,在创建LV的时候,偶尔会遇到“Volume group "x ...
- CF1208D
CF1208D 题意: 给你一个数组,要求支持单点修改和单点查询 解法: 直接线段树搞一搞就没了. CODE: #include<iostream> #include<cstdio& ...
- tecplot 把散点绘成曲面图【转载】
转载自:http://blog.sina.com.cn/s/blog_a319f5ff0101q6s8.html 找了好久,终于自己研究出来,如何使用tecplot绘制曲面图了 第一步:数据的整理 如 ...
- elasticsearch各种服务链接
查看elasticsearch信息http://localhost:9200/ 查看某个索引信息http://localhost:9200/index_name例如:http://localhost: ...
- js中的splice方法和slice方法简单总结
slice:是截取用的 splice:是做删除 插入 替换用的 slice(start,end): 参数: start:开始位置的索引 end:结束位置的索引(但不包含该索引位置的元素) 例如: va ...