
Hi, Is there a way to join a thread automatically to the main thread when it finishes execution? In my case, the main thread does not call the "thread->join()", so even if the work of the thread is over, the thread never joins to the main thread. What is the technique usually used to make a thread join to its main thread, so as to avoid wastage of resources. Thanks, Lloyd ___________________ Happy New Year 2010

Is there a way to join a thread automatically to the main thread when it finishes execution?
In my case, the main thread does not call the "thread->join()", so even if the work of the thread is over, the thread never joins to the main thread. What is the technique usually used to make a thread join to its main thread, so as to avoid wastage of resources.
What kind of waist? I guess the name of the function mislead you. join() in this context means "wait" - nothing more.

This is the way I create new thread 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? Thanks, Lloyd
What kind of waist? I guess the name of the function mislead you. join() in this context means "wait" - nothing more. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
___________________ Happy New Year 2010

On Fri, Jan 8, 2010 at 4:41 PM, Lloyd
This is the way I create new thread
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?
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.
Thanks, Lloyd
What kind of waist? I guess the name of the function mislead you. join() in this context means "wait" - nothing more. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
___________________ Happy New Year 2010
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

yes I too feel that you are right, and I think that the problem is with the shared_ptr (shared_from_this()) . Can anyone suggest a way to reduce the shared_ptr reference count at the end of the thread execution, so that all referece will be reduced and the respective destructor will be called.
Thanks a lot,
Lloyd
----- Original Message -----
From: Shiou Ming Lee
To: boost-users@lists.boost.org
Sent: Friday, January 08, 2010 3:20 PM
Subject: Re: [Boost-users] Automatic thread join
On Fri, Jan 8, 2010 at 4:41 PM, Lloyd

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.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On Friday 08 January 2010, Lloyd wrote:
Hi,
Is there a way to join a thread automatically to the main thread when it finishes execution?
You can detach the thread. There's a member function, and it also happens automatically when the boost::thread object is destroyed. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAktHNr0ACgkQ5vihyNWuA4Uh4wCfYk7eM0DRF/vBdEv+MwBJpxXC ULcAnib9tPNi42/A98gQVpA1nyVo5jPo =8cyw -----END PGP SIGNATURE-----
participants (5)
-
Frank Mori Hess
-
Igor R
-
Lloyd
-
Ovanes Markarian
-
Shiou Ming Lee