boost库timed_wait、notify_one配合使用
来源:互联网 发布:骇战网络暴力 编辑:程序博客网 时间:2024/06/01 07:42
// scoped_lock使用 //timed_wait、notify_one配合使用,阻塞等到,接收到消息自动运行相当于消息锁//// #include "stdafx.h" #include <boost/thread/thread.hpp>#include <boost/thread/mutex.hpp>#include <boost/thread/condition.hpp>#include <boost/date_time/posix_time/posix_time.hpp>#include <iostream>#include<windows.h>using namespace std;using namespace boost;boost::mutex io_mutex;boost::condition_variable_any test_condition;void test(int num) {for (;;) {boost::mutex::scoped_lock lock(io_mutex);cout << "等待者" << num << endl;//阻塞等待30s,阻塞时自动解锁if ( test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(30) ) ) { cout << num << "成功接收到通知" << endl;} else{cout << num << "没有等待到通知" << endl;}}}void notify(){while(1){cout << "发送通知"<<endl;system("pause");//test_condition.notify_all(); test_condition.notify_one(); }}int main(int argc, char* argv[]){boost::thread thrd11(bind(test, 1));boost::thread thrd12(bind(test, 2));boost::thread thrd13(notify);thrd11.join();thrd12.join();thrd13.join();system("pause"); return 0;}
0 0
- boost库timed_wait、notify_one配合使用
- 【Boost】boost库中thread多线程详解7——wait与timed_wait简单例子
- 【Boost】boost库中thread多线程详解7——wait与timed_wait简单例子
- boost库之bind(与function配合使用)
- boost::asio使用async_read_until配合boost::regex读取数据
- boost split和lexical_cast配合使用处理文本
- Boost条件变量condition_variable_any的notify_one和wait与mfc中CEvent和WaitForSingleObject区别
- log4cxx配合boost::format
- log4cxx配合boost::format
- log4cxx配合boost::format
- boost::asio::io_service和io_service::work和boost::thread_group配合使用
- boost::asio::io_service和io_service::work和boost::thread_group配合使用
- Condition Variable notify_one notify_all
- boost 库Regex使用
- 开始使用Boost库
- boost使用库资源
- boost库json_parser使用
- Boost Serialization库使用
- 结构体在内存中的存储方式
- 小球弹跳:UI2-2
- dark认为比较好比较全的java反射详解示例
- C# 集合类 :(Array、 Arraylist、List、Hashtable、Dictionary、Stack、Queue)
- Android中单击空白区域隐藏键盘
- boost库timed_wait、notify_one配合使用
- onTouch,onClick,onLongClick事件发生先后顺序和关联
- 在ECLIPSE导入项目后提示::具有不受限制的类路径容器:“JRE 系统库 [JVM 1.6.0]”
- 理解A*寻路算法具体过程
- js框架jquery实现幸运大转盘抽奖程序代码,兼容多种浏览器
- 打靶问题
- java中this的用法
- 11.spritekit征程——结尾篇之节点的狂欢
- Spring + Hibernate异常