Boost logo

Boost Users :

Subject: Re: [Boost-users] Automatic thread join
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2010-01-08 05:57:35


>
>
>> void TCPConnection::MyFunction()
>> {
>> ....
>> boost::thread *th=new
>> boost::thread(boost::bind(&TCPConnection::ExecutionRoutine,shared_from_this(),Command));
>> ...
>> }
>>
>> when the ExecutionRoutine() finishes its execution, naturally the
>> destructor of the TCPConnection class has to be called. But it never gets
>> called! But if call a join from someware else (th->join()) the destructor of
>> the TCPConnection class gets called and the connection is terminated,
>> otherwise the it continues in the connected state!!
>>
>> What could be the mistake I am doing?
>>
>
> You can use the thread_group and calling join_all.

> If not mistaken that's the normal behavior which requires parent thread to
> join() its child threads, for proper release of thread resources, and I
> don't think it is specific to Boost.Thread. I might be wrong though.
>
Implicitly join threads is a very bad style since it might cause deadlocks
if e.g. parent thread holds the resource the child thread waits on and the
parent waits on child to finish. You think in way Java and .NET introduced
there threading (Foreground/Background threads). But in any operating system
threads do not wait on children implicitly, which is far better and flexible
solution.



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