Boost logo

Boost Users :

Subject: Re: [Boost-users] Crash in boost::function destructor in my thread pool implementation
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2012-09-18 00:23:31


AMDG

On 09/17/2012 01:01 PM, Marcin Adamski wrote:
> Hi
>
> I am trying to implement thread pool in C++ using pthread and boost::function as a wrapper for any function that client would like to call in the worker thread. I want to encapsulate logic related to threads management in one object which is taking ownership of these threads. That means whenever this object is destroyed, threads must be stopped and cleaned up.
>
> I've been testing my code and it turns out that I get segmentation fault when I destroy WorkerThreadManager object while there is boost::function processed in worker thread. See the code in attachment and backtrace from GDB below.
>
> I don't really understand why it happens, as far as I know boost::function is copyable, so once I get a copy of it from the queue, I can pop() it and even destroy whole queue (I prooved that in some small test) and then call the function's copy. So why is destruction of std::queue< boost::function<void() > causing problems? Or maybe it is something else.
>

You have a race condition in your destructor.
pthread_cancel, does *not* wait for the thread
to terminate. You need to join the threads
before the queue is destroyed.

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net