"Simple" graceful stop of boost thread

Hello, I know this is possible, just don't have all the templates at the ready. Graceful way of stopping a boost worker thread from running. I am familiar with C# .NET threading, I can fairly simply signal an AutoResetEvent, for instance, and wait a nominal period for it to be set. Afterwards, this tells the thread to stop what it's doing at an opportune moment. Meanwhile the parent thread is joining the worker thread(s) back up. Something like that. I just need a few tips what concept to link up: i.e. mutex? shared_mutex? lock? shared_lock? I'll have a start_work, stop_work, and working type motif going on. I call start_work to start the working thread going. Simple enough. I will call stop_work to signal the worker thread(s) to stop and join up with the worker threads. Somewhere in between the worker thread is polling the lock does it need to stop, do it's work, then back around again. And so on... Thank you... Regards, Michael Powell

On Wed, Apr 10, 2013 at 6:00 PM, Michael Powell
Hello,
I know this is possible, just don't have all the templates at the ready. Graceful way of stopping a boost worker thread from running.
Something very nearly like this is along the lines what I am looking for (untested): http://stackoverflow.com/questions/8538575/is-there-an-easy-way-to-implement... Using this abstraction, very easily set it up to coordinate the worker producer thread with controller wanting to stop.
I am familiar with C# .NET threading, I can fairly simply signal an AutoResetEvent, for instance, and wait a nominal period for it to be set.
Afterwards, this tells the thread to stop what it's doing at an opportune moment. Meanwhile the parent thread is joining the worker thread(s) back up.
Something like that. I just need a few tips what concept to link up: i.e. mutex? shared_mutex? lock? shared_lock?
I'll have a start_work, stop_work, and working type motif going on. I call start_work to start the working thread going. Simple enough. I will call stop_work to signal the worker thread(s) to stop and join up with the worker threads.
Somewhere in between the worker thread is polling the lock does it need to stop, do it's work, then back around again. And so on...
Thank you...
Regards,
Michael Powell

Le 11/04/13 02:37, Michael Powell a écrit :
On Wed, Apr 10, 2013 at 6:00 PM, Michael Powell
mailto:mwpowellhtx@gmail.com> wrote: Hello,
I know this is possible, just don't have all the templates at the ready. Graceful way of stopping a boost worker thread from running.
Something very nearly like this is along the lines what I am looking for (untested):
http://stackoverflow.com/questions/8538575/is-there-an-easy-way-to-implement...
Is this something easy to adapt to your needs? I don't think you need to use a blocking wait until the the worker thread is stopped, but just need to protect a variable that tell you that the worker thread is stopped. You could also use Boost.Atomic, synchronized_value.
Using this abstraction, very easily set it up to coordinate the worker producer thread with controller wanting to stop.
I am familiar with C# .NET threading, I can fairly simply signal an AutoResetEvent, for instance, and wait a nominal period for it to be set.
Afterwards, this tells the thread to stop what it's doing at an opportune moment. Meanwhile the parent thread is joining the worker thread(s) back up.
Something like that. I just need a few tips what concept to link up: i.e. mutex? shared_mutex? lock? shared_lock?
mutex and the associated lock types unique_lock/lock_guard.
I'll have a start_work, stop_work, and working type motif going on. I call start_work to start the working thread going. Simple enough. I will call stop_work to signal the worker thread(s) to stop and join up with the worker threads.
Somewhere in between the worker thread is polling the lock does it need to stop, do it's work, then back around again. And so on...
Let me know if I'm missing something. Best, Vicente

Hi,
If you want a thread to be stopped gracefully, I think the best way is
to use boost thread's interruption :
http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#...
You can specify interruption points in the stoppable thread by
boost::this_thread::interruption_point(), and there are several
predefined interruption points like sleep
(http://www.boost.org/doc/libs/1_53_0/doc/html/thread/thread_management.html#...).
"A running thread can be interrupted by invoking the interrupt()
member function of the corresponding boost::thread object. When the
interrupted thread next executes one of the specified interruption
points (or if it is currently blocked whilst executing one) with
interruption enabled, then a boost::thread_interrupted exception will
be thrown in the interrupted thread."
HTH,
Gabor
On Thu, Apr 11, 2013 at 1:00 AM, Michael Powell
Hello,
I know this is possible, just don't have all the templates at the ready. Graceful way of stopping a boost worker thread from running.
I am familiar with C# .NET threading, I can fairly simply signal an AutoResetEvent, for instance, and wait a nominal period for it to be set.
Afterwards, this tells the thread to stop what it's doing at an opportune moment. Meanwhile the parent thread is joining the worker thread(s) back up.
Something like that. I just need a few tips what concept to link up: i.e. mutex? shared_mutex? lock? shared_lock?
I'll have a start_work, stop_work, and working type motif going on. I call start_work to start the working thread going. Simple enough. I will call stop_work to signal the worker thread(s) to stop and join up with the worker threads.
Somewhere in between the worker thread is polling the lock does it need to stop, do it's work, then back around again. And so on...
Thank you...
Regards,
Michael Powell
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (3)
-
Gábor Márton
-
Michael Powell
-
Vicente J. Botet Escriba