On Sat, Sep 11, 2010 at 11:00:56PM +0200, Boris Schaeling wrote:
Is a boost::thread object automatically detached when a thread terminates?
I read in the Boost.Thread 1.44 documentation: "If the thread of execution represented by the boost::thread object has already completed, or the boost::thread object represents Not-a-Thread, then join() returns immediately."
Is it really required to call join() or timed_join()? Or can I also call joinable() which would return false then?
The four ways of cleaning up properly after a thread with Boost.Thread are: 1) detach() to make the boost::thread object not refer to a thread of execution anymore and any associated thread resources properly released; 2) have the boost::thread object die a natural death (upon which it will detach() in the destructor, resulting in 1); 3) join(), after return of the boost::thread will not refer to a thread of execution and the thread will have been cleaned up; 4) a successful timed_join(), with the same results as 3). Note that in no case it's sane to do things like { new thread(&f); }, as you're not only leaking a thread object, but also OS thread primitives, resulting in zombies and resource exhaustion. I hope this clears some things up. -- Lars Viklund | zao@acc.umu.se