0

I implement a "thread_pool" which causes hang in pthread_cond_signal. I'm curious why it happens?

This is the gdb stack:

(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

Here is the code snippet:

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 Answers1

0

I did destroy the cond and lock then thread pool was destructed, while timer trying to dispatch some task to the thread pool. I forgot to delete timer before thread pool was destructed.