随笔 - 7  文章 - 6  trackbacks - 0
<2009年2月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
1234567

常用链接

留言簿(1)

随笔档案

文章分类

搜索

  •  

积分与排名

  • 积分 - 32762
  • 排名 - 600

最新评论

阅读排行榜

评论排行榜

boost的线程库不能强行终止,所以通过time_wait可以让其自然的结束

 1
 #include <iostream>
 2 #include <boost/thread/thread.hpp>
 3 #include <boost/thread/mutex.hpp>
 4 #include <boost/thread/condition.hpp>
 5 #include <boost/date_time/posix_time/posix_time.hpp>
 6 
 7 using namespace std;
 8 using namespace boost;
 9 
10 boost::mutex test_mutex;
11 boost::condition_variable test_condition;
12 
13 void test() {
14 
15     for (;;) {
16         
17         boost::mutex::scoped_lock lock(test_mutex);
18         if (test_condition.timed_wait(lock, get_system_time() + posix_time::seconds(3))) {
19             cout << "成功接收到通知" << endl; //这里加个break就结束了
20         } else {
21             cout << "没有等待到通知" << endl;
22         }
23 
24     }
25 }
26 
27 int main() {
28 
29     boost::thread test_thread(test);
30 
31     for (;;) {
32         ::system("PAUSE");
33         cout << "开始发送通知" << endl;
34         test_condition.notify_one();
35     }
36 
37 }

posted on 2009-02-19 17:18 许海斌 阅读(5075) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理