为什么它在pthread_cond_signal中被绞死

时间:2015-11-20 13:34:56

标签: c++ locking signals threadpool mutex

我实现了" thread_pool"这导致在pthread_cond_signal中挂起。我好奇为什么会这样?

这是gdb堆栈:

(gdb)bt

#0  0x00007fc1ac92ee24 in __lll_lock_wait () from /opt/compiler/gcc-       4.8.2/lib/libpthread.so.0
#1  0x00007fc1ac92ccb1 in pthread_cond_signal@@GLIBC_2.3.2 () from /opt/compiler/gcc-4.8.2/lib/libpthread.so.0
#2  0x00000000004ac736 in ThreadPool::dispatch (this=0x415e0a0, method=0x40d1080) at ../../common/thread_pool.cc:76

以下是代码段:

int ThreadPool::dispatch(Method* method) {
    pthread_mutex_lock(&_method_lock);
    _method_list.push_back(method);
    pthread_cond_signal(&_method_cond);
    pthread_mutex_unlock(&_method_lock);

    return 0;
}

ThreadPool::Method* ThreadPool::get() {
    Method* method = NULL;

    pthread_mutex_lock(&_method_lock);

    if (_method_list.empty()) {
        struct timeval now;
        struct timespec timeout;
        gettimeofday(&now, 0);
        timeout.tv_sec = now.tv_sec + 1;
        timeout.tv_nsec = now.tv_usec * 1000;

        pthread_cond_timedwait(&_method_cond, &_method_lock, &timeout);
    }

    if (!_method_list.empty()) {
        method = _method_list.front();
        _method_list.pop_front();
    }
    pthread_mutex_unlock(&_method_lock);

    return method;
}

1 个答案:

答案 0 :(得分:0)

我确实销毁了cond并锁定然后线程池被破坏,而计时器试图将一些任务调度到线程池。我忘了在线程池被破坏之前删除计时器。