proper way to wait for threads ends

Hello I m writting a program which create threads following commands received on a socket connection. When exiting, I'd like to wait that all dynamically created threads have ended their jobs. I thought about using a shared variable (with mutual exclusion) that threads would increment at start and decrement at end. And then the program would stop, loop on sleep() while this shared variable is still > 0. Is there a better way to wait for the end of threads ?

Look at thread::join(), thread_group::join()> I m writting a program which create threads following commands received > on a socket connection.> When exiting, I'd like to wait that all dynamically created threads have > ended their jobs.> I thought about using a shared variable (with mutual exclusion) that > threads would increment at start and decrement at end.> And then the program would stop, loop on sleep() while this shared > variable is still > 0.> > Is there a better way to wait for the end of threads ? _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx

Igor R. wrote:
Look at thread::join(), thread_group::join()
This implies that I keep a reference of all created threads. Since the program aims to run 24/24, 7/7, and could create thousands of threads before the program receive the command to stop, I thought it was a bad idea to keep the threads references in a thread_group.

Axel wrote:
Igor R. wrote:
Look at thread::join(), thread_group::join()
This implies that I keep a reference of all created threads. Since the program aims to run 24/24, 7/7, and could create thousands of threads before the program receive the command to stop, I thought it was a bad idea to keep the threads references in a thread_group.
Ah, I see. In that case, I would second the recommendation by Alex MDC to use a condition variable instead of your sleep. KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com

Hello all, I probably miss something, but I can't find the above conversion. There're from_time_t(), to_tm() helpers etc., but no to_time_t() ? I know I can calculate it as the number of seconds since "epoche", but I believe there should be some more trivial way. Thanks! _________________________________________________________________ Connect to the next generation of MSN Messenger http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&source=wlmailtagline

Using these two isn't perfect but better than going back to epoch: tm to_tm(ptime); time_t mktime ( struct tm * timeptr ); ________________________________ From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Igor R. Sent: 15 May 2008 16:40 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [date_time] ptime to time_t Hello all, I probably miss something, but I can't find the above conversion. There're from_time_t(), to_tm() helpers etc., but no to_time_t() ? I know I can calculate it as the number of seconds since "epoche", but I believe there should be some more trivial way. Thanks! ________________________________ Connect to the next generation of MSN Messenger Get it now! <http://imagine-msn.com/messenger/launch80/default.aspx?locale=en-us&sou rce=wlmailtagline> ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________ ****************************************************************************** "This message and any attachments are solely for the intended recipient and may contain confidential and privileged information. If you are not the intended recipient, any disclosure, copying, use, or distribution of the information included in this message and any attachments is prohibited. If you have received this communication in error, please notify us by reply e-mail and immediately and permanently delete this message and any attachments. Thank you." Interactive Transaction Solutions Ltd (2473364 England) Registered Office: Systems House, Station Approach Emsworth PO10 7PW ********************************************************************** Ce message �lectronique contient des informations confidentielles � l'usage unique des destinataires indiqu�s, personnes physiques ou morales. Si vous n'�tes pas le destinataire voulu, toute divulgation, copie, ou diffusion ou toute autre utilisation de ces informations, est interdite. Si vous avez re�u ce message �lectronique par erreur, nous vous remercions d'en avertir son exp�diteur imm�diatement par email et de d�truire ce message ainsi que les �l�ments attach�s. Interactive transaction Solutions SAS- France (RCS Pontoise : 489 397 877) Si�ge social : Parc Saint Christophe, 10, Avenue de l�Entreprise 95865 Cergy-Pontoise Cedex ______________________________________________________________________ This email has been scanned by the MessageLabs Email Security System. For more information please visit http://www.messagelabs.com/email ______________________________________________________________________

Patrick Loney wrote:
Using these two isn’t perfect but better than going back to epoch:
tm to_tm(ptime);
time_t mktime ( struct tm * timeptr );
That's probably fairly inefficient. Part of the reason this isn't in the library is that there's a number of safety issues when converting back to time_t (and it hasn't come up much). The code below points out those issues. This code is completely untested: //! Function that converts a ptime into a time_t inline std::time_t to_time_t(ptime t) { //if ptime is not_a_date_time or an infinity there's no conversion if (t.is_special()) { throw some_exception_type("conversion undefined"); } ptime time_t_epoch(gregorian::date(1970,1,1)); //if ptime is less than 1970-1-1 conversion will fail if (t < time_t_epoch) { throw some_exception_type("conversion undefined -- less than valid time_t epoch"); } time_duration td = t - time_t_epoch(); //cast likely needed to down covert from 64 to 32 bit rep //NOTE: all fractional seconds are truncated below... return static_caststd::time_t(td.total_seconds()); } HTH, Jeff

Part of the reason this isn't in the library is that there's a number of safety issues when converting back to > time_t
Please, note that all the issues you mentioned are relevant to the to_tm() function as well (except it has 1900-based year, while time_t is 1970-based). _________________________________________________________________ News, entertainment and everything you care about at Live.com. Get it now! http://www.live.com/getstarted.aspx

Axel
I m writting a program which create threads following commands received on a socket connection. When exiting, I'd like to wait that all dynamically created threads have ended their jobs. I thought about using a shared variable (with mutual exclusion) that threads would increment at start and decrement at end. And then the program would stop, loop on sleep() while this shared variable is still > 0.
Is there a better way to wait for the end of threads ?
Call t.join() for each thread t. Alternatively put them all in a thread_group and call join_all(). Anthony -- Anthony Williams | Just Software Solutions Ltd Custom Software Development | http://www.justsoftwaresolutions.co.uk Registered in England, Company Number 5478976. Registered Office: 15 Carrallack Mews, St Just, Cornwall, TR19 7UL

Axel wrote:
I m writting a program which create threads following commands received on a socket connection. When exiting, I'd like to wait that all dynamically created threads have ended their jobs. I thought about using a shared variable (with mutual exclusion) that threads would increment at start and decrement at end. And then the program would stop, loop on sleep() while this shared variable is still > 0.
Is there a better way to wait for the end of threads ?
You could join all the threads. http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_management.html#... KevinH -- Kevin Heifner heifner @ ociweb.com http://heifner.blogspot.com Object Computing, Inc. (OCI) www.ociweb.com

I think your idea is pretty good, however instead of using sleep to
periodically check the shared counter, you might try using a condition
variable to notify the main thread that a worker thread has finished.
The example in the Boost.Thread documentation looks very close to what you
want.
Alex
On 15/05/2008, Axel
Hello
I m writting a program which create threads following commands received on a socket connection. When exiting, I'd like to wait that all dynamically created threads have ended their jobs. I thought about using a shared variable (with mutual exclusion) that threads would increment at start and decrement at end. And then the program would stop, loop on sleep() while this shared variable is still > 0.
Is there a better way to wait for the end of threads ?
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (7)
-
Alex MDC
-
Anthony Williams
-
Axel
-
Igor R.
-
Jeff Garland
-
Kevin Heifner
-
Patrick Loney