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
许海斌 阅读(5103)
评论(0) 编辑 收藏 引用