1
我的問題是:是否有某種特定的需要做的事情(如調用notify_all?)在封裝boost :: condition_variable的線程的析構函數中。下面的代碼生成此斷言當測試調用析構函數:提升condition_variable銷燬聲明
cond_var: /usr/include/boost/thread/pthread/condition_variable_fwd.hpp:38: 的boost :: condition_variable ::〜condition_variable() :聲明 '!pthread_cond_destroy(& cond)'失敗。中止
#include <boost/thread/mutex.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread.hpp>
class Test {
public:
~Test() { /* ??? */ }
int getI() {
boost::mutex::scoped_lock lock(mtx);
cond_var.wait(lock);
return i;
}
void putI() {
i=10;
cond_var.notify_one();
}
int i;
boost::mutex mtx;
boost::condition_variable cond_var;
} t;
void runPut() {
for(;;) {
t.getI();
}
}
void runGet() {
for(;;) {
t.putI();
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
}
int main() {
boost::thread t1(&runPut);
boost::thread t2(&runGet);
boost::this_thread::sleep(boost::posix_time::seconds(5));
return 0;
}
(GDB)BT
0 0x00007ffff70c4d05 in raise() from /lib/x86_64-linux-gnu/libc.so.6
1 0x00007ffff70c8ab6 in abort() from /lib/x86_64-linux-gnu/libc.so.6
2 0x00007ffff70bd7c5 in __assert_fail() from /lib/x86_64-linux-gnu/libc.so.6
3 0x0000000000406a35 in boost::condition_variable::~condition_variable (this=0x6104d0,
__in_chrg=<value optimized out>) at /usr/include/boost/thread/pthread/condition
_variable_fwd.hpp:38
4 0x0000000000406f42 in Test::~Test (this=0x6104a0, __in_chrg=<value optimized out>) at cond_var.cpp:19
5 0x00007ffff70ca961 in exit() from /lib/x86_64-linux-gnu/libc.so.6
6 0x00007ffff70aff06 in __libc_start_main() from /lib/x86_64-linux-gnu/libc.so.6
7 0x0000000000405899 in _start()
該平臺是ubuntu Linux 11.04,具有boost-1.42,gcc 4.52 – long404