Boost condition_variable уничтожение заявить - PullRequest
1 голос
/ 28 сентября 2011

Мой вопрос: есть ли что-то конкретное, что мне нужно сделать (например, вызов notify_all?) В деструкторе потока, который инкапсулирует boost :: condition_variable. Следующий код генерирует это утверждение при вызове деструктора Test:

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 ()

1 Ответ

2 голосов
/ 28 сентября 2011

Вам нужно присоединиться к двум темам, которые вы создали, прежде чем выйти.Добавьте глобальную логическую переменную с именем say, 'stop', инициализированную в false, и пусть t1 и t2 проверяют, что она ложна на каждой итерации:

bool stop = false;

void runPut () {
    while( !stop ) {
        t.getI();
    }
}

void runGet () {
    while( !stop ) {
        t.putI();
        boost::this_thread::sleep(boost::posix_time::milliseconds(100));
    }
}

Затем после сна в main установите ее в true, и вызовитеобъединение в t1 и t2.

boost::this_thread::sleep(boost::posix_time::seconds(5));
stop = true;
t1.join();
t2.join();

В противном случае условные переменные уничтожаются, пока они еще используются.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...