boost的線程庫不能強(qiáng)行終止,所以通過time_wait可以讓其自然的結(jié)束
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; //這里加個(gè)break就結(jié)束了
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 << "開始發(fā)送通知" << endl;
34 test_condition.notify_one();
35 }
36
37 }
posted on 2009-02-19 17:18
許海斌 閱讀(5117)
評論(0) 編輯 收藏 引用