Boost logo

Boost Users :

Subject: Re: [Boost-users] [thread] boost::thread automatically detached when thread terminates?
From: Lars Viklund (zao_at_[hidden])
Date: 2010-09-11 17:22:12


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_at_[hidden]

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