[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]> wrote: > I thought about the locking approach, but this will all eventually be > wrapped into a Windows services tool that will shutdown the service > gracefully.
I don't see how that removes the need for locking when changing shared variables? > > The way services work, the process needs to ping the service control > manager once in a while when it is still running. So, if a thread > didn't shutdown after a join() in x number of seconds, Windows would > assume the program is not responding. And who knows how long a > worker thread will spend on one task before saying hello again. This > is essentially how they will talk: > > Worker Threads <-> Master Thread / Services Callback Thread <-> > Windows Services Sorry, but I don't really follow that. > > I can say I found something similar to your idea that works nicely. > Thanks to the 'async' command, I can use a shared variable and adjust > that variable after it asynchronously joins the threads. Yes, very > much in the realm of hacks and bad ideas, but it works. With the > verbage of this thing, it really sounds like nonsense spawning > threads from the master and joining them to another child. I'm not sure that it does work (see comments in code),except by coincidence. It certainly doesn't work in later versions of Perl. > > <xmp> > # Test Threads > > use threads; > use threads::shared; > use Win32; > use strict; > > use vars qw ($counter $thr $thread $threads); Prefer declare variables lexically (i.e. my ...), unless you actually need to do otherwise. > my $thread_shutdown : > shared; > > Log ("Starting Thread Test"); > > $thr = threads->create (\&ChildThread, 0); > > $counter = 0; > while ($counter++ < 60) { > sleep (1); > Log ('This is the main thread speaking: '.localtime(time())); } > > # Signal shutdown state > $thread_shutdown = 1; > > # Asynchronously join threads. > $threads = threads->list(); Note that $threads contains a single thread object ... > async { > my $result; > foreach $thread ($threads) { ... this will therefor be a problem if you have more than one thread. > $result = $thread->join (); > Log ("Result for thread ".$thread->tid.": ".$result); > } > # Signal final shutdown state > $thread_shutdown = 2; > }; Also you don't join the async thread. Older versions of Perl might not complain about that, but later ones will. > > # Await shutdown state change > while ($thread_shutdown < 2) { > Log ("Awaiting thread death."); > sleep (10); > } > > Log ("Application is ended."); > > # Functions > > sub Log { my( $Message ) = @_; print $Message."\n"; } > > sub ChildThread { > my ($counter) = @_; > > while (!$thread_shutdown) { > Log ('This is the child thread speaking: '.localtime(time())); > Win32::Sleep ((rand(12)+1) * 500); > } > Log ('Thread has ended.'); > return 10; > } > </xmp> I don't see what creating a separate thread to do wait for your child/worker threads to terminate, then waiting for it to terminate gives you. It seems to be the very opposite of async. HTH -- Brian Raven ========================================= Atos Euronext Market Solutions Disclaimer ========================================= The information contained in this e-mail is confidential and solely for the intended addressee(s). Unauthorised reproduction, disclosure, modification, and/or distribution of this email may be unlawful. If you have received this email in error, please notify the sender immediately and delete it from your system. The views expressed in this message do not necessarily reflect those of Atos Euronext Market Solutions. Atos Euronext Market Solutions Limited - Registered in England & Wales with registration no. 3962327. Registered office address at 25 Bank Street London E14 5NQ United Kingdom. Atos Euronext Market Solutions SAS - Registered in France with registration no. 425 100 294. Registered office address at 6/8 Boulevard Haussmann 75009 Paris France. L'information contenue dans cet e-mail est confidentielle et uniquement destinee a la (aux) personnes a laquelle (auxquelle(s)) elle est adressee. Toute copie, publication ou diffusion de cet email est interdite. Si cet e-mail vous parvient par erreur, nous vous prions de bien vouloir prevenir l'expediteur immediatement et d'effacer le e-mail et annexes jointes de votre systeme. Le contenu de ce message electronique ne represente pas necessairement la position ou le point de vue d'Atos Euronext Market Solutions. Atos Euronext Market Solutions Limited Société de droit anglais, enregistrée au Royaume Uni sous le numéro 3962327, dont le siège social se situe 25 Bank Street E14 5NQ Londres Royaume Uni. Atos Euronext Market Solutions SAS, société par actions simplifiée, enregistré au registre dui commerce et des sociétés sous le numéro 425 100 294 RCS Paris et dont le siège social se situe 6/8 Boulevard Haussmann 75009 Paris France. ========================================= _______________________________________________ ActivePerl mailing list ActivePerl@listserv.ActiveState.com To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs