boost::condition_variable、boost::time_wait使用方法
2016-09-01 16:38
357 查看
boost 的线程库不能强行终止,所以通过 time_wait 可以让其自然的结束。
// BoostTest.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <iostream> #include <boost/thread/thread.hpp> #include <boost/thread/mutex.hpp> #include <boost/thread/condition.hpp> #include <boost/date_time/posix_time/posix_time.hpp> using namespace std; using namespace boost; boost::mutex test_mutex; boost::condition_variable test_condition; //#pragma comment(lib,"../../lib/x86/boost/libboost_thread-vc100-mt-gd-1_55.lib") //#pragma comment(lib,"..//..//lib//x86//boost//libboost_thread-vc100-mt-gd-1_55.lib") ////../../lib/x86/boost/libboost_thread-vc100-mt-gd-1_55.lib;../../lib/x86/boost/libboost_date_time-vc100-mt-gd-1_55.lib;../../lib/x86/boost/libboost_system-vc100-mt-gd-1_55.lib;../../lib/x86/boost/libboost_chrono-vc100-mt-gd-1_55.lib void test() { for (;;) { boost::mutex::scoped_lock lock(test_mutex); if (test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(3))) { cout << "成功接收到通知" << endl; //这里加个break就结束了 } else { cout << "没有等待到通知" << endl; } } } int _tmain(int argc, _TCHAR* argv[]) { boost::thread test_thread(test); for (;;) { ::system("PAUSE"); cout << "开始发送通知" << endl; test_condition.notify_one(); } return 0; }
相关文章推荐
- boost::condition_variable、boost::time_wait使用方法
- QWaitCondition 的正确使用方法
- boost::condition_variable的使用
- boost中的condition_variable (条件变量)的使用
- JAVA5多线程---Condition使用---线程通信 --wait及notify方法
- boost综合使用<function,bind,thread,mutex,condition_variable,shared_ptr>
- QWaitCondition 的正确使用方法
- [转]QWaitCondition 的正确使用方法
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠
- struct tm 和 time_t 时间和日期的使用方法(转)
- struct tm 和 time_t 时间和日期的使用方法
- Boost::Regex 使用方法 (英文)
- 最简实例说明wait、notify、notifyAll的使用方法
- struct tm 和 time_t 时间和日期的使用方法(转)
- 修改Tcp的Time-Wait的方法
- pthread_cond_wait()的使用方法
- [原创]DateTime在使用 format Custom Date and Time Format Strings时遇到的问题和解决方法
- struct tm 和 time_t 时间和日期的使用方法(转)
- struct tm 和 time_t 时间和日期的使用方法(转)
- linux内核的 等待队列 使用方法,wait_queue_head_t,进程休眠