Boost logo

Threads-Devel :

Subject: Re: [Threads-devel] Leak on not joined thread / Linux / Boost 1.34.1
From: Anthony Williams (anthony_at_[hidden])
Date: 2009-02-16 11:19:51


At Mon 16 Feb 2009 12:59:38 UTC, Emilio Pombo <epombo_at_[hidden]> wrote:

> After some code comments -the old style leak find method school ;)-
> the result was really surprising for me... the leaks were produced
> after each thread launch, so at this point I quickly change
> Boost::Thread by POSIX Thread -the default option basically on
> Asterisk development-, the problem was still there. After that I
> released that on some flows of execution the module wasn't leaking,
> and that flows matched with ones were I have to join the thread (on
> some cases my requirements force me to avoid join and terminate
> thread directly from launching function with no join from another
> thread).

You have to explicitly join or detach every thread to ensure that the
thread management data is correctly cleaned up.

> Finally solved the problem with POSIX Thread, creating always
> detachable threads -non joinable-, and doing sync with a semaphore.
> I think that the problem on boost::thread was significantly the same
> as with POSIX, but you can't access this kind of details on
> boost::thread, or at least I think you must be "protected" by
> surround implementation from a leak if you start a thread that you
> never execute method ::join() from another thread -I searched on
> boost doc, I founded a ::detach() method, but on my version of boost
> at least wasn't available, may be executing this on non joinable
> flows could be the solution-... any comments?

In boost 1.35 or later there is a boost::thread::detach function. In
boost 1.34, the thread is detached when you destroy the boost::thread
object. So, only keep around the boost::thread object if you intend to
join with the thread; otherwise just make the boost::thread object
temporary:

void launch_thread()
{
     boost::thread t(some_func);
} // t is destroyed here; thread is detached.

You could avoid giving the thread variable a name at all:

boost::thread(some_func); // temporary destroyed at end of statement
=> detached thread

Anthony

-- 
Anthony Williams
Author of C++ Concurrency in Action | http://www.manning.com/williams
just::thread C++0x thread library   | http://www.stdthread.co.uk
Custom Software Development         | http://www.justsoftwaresolutions.co.uk
Just Software Solutions Ltd, Registered in England, Company Number 5478976.
Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK

Threads-Devel list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk