Boost logo

Boost Users :

Subject: Re: [Boost-users] [Thread] Timed join returning true before thread terminated
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2012-03-14 18:23:30


On 14/03/12 20:09, John Rocha wrote:
> I have been able to extract the thread start/stop logic from our code
> base into
> a standalone program that illustrates the problem. Even this is still
> sort of
> long, 900 lines or so.

Thanks for the example. Your problem is that you are overlaying TWO
interruption mechanisms --- boost::thread::interrupt() and your own
m_shutdown flag.

thread::join() is an interruption point, so if your thread sees the
m_shutdown flag before the boost::thread::interrupt(), then it will pick
up the interrupt when it calls join() on its own worker threads.

I would suggest that you avoid the use of m_shutdown, since it is
redundant. Also, wrap your calls to join() in scope with a
boost::this_thread::disable_cancellation object so that the join cannot
be interrupted.

> void shutdown(const std::string &s_caller) {
> EE_LOG_MSG(EE_TRACE, "%s shutting down %s",
> s_caller.c_str(), m_name.c_str());
>
> ptime start_time(microsec_clock::local_time());
>
> m_shutdown = true;
> m_thread.interrupt();
> m_thread.join();

> inline void check_for_shutdown () {
> boost::this_thread::interruption_point();
>
> if (m_shutdown) {
> throw boost::thread_interrupted();
> }
> }

Anthony

-- 
Author of C++ Concurrency in Action     http://www.stdthread.co.uk/book/
just::thread C++11 thread library             http://www.stdthread.co.uk
Just Software Solutions Ltd       http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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