Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Win32 ActiveState Perl 5.8 - use threads

by bdimych (Monk)
on Jan 03, 2005 at 09:13 UTC ( #418898=perlquestion: print w/replies, xml ) Need Help??
bdimych has asked for the wisdom of the Perl Monks concerning the following question:

Hi All Whether it is possible to determine that thread is finished not using additional variables through threads::shared
use threads; my $thread = async {sleep 10; print "Ok\n"}; my $i = 0; while (1) { $i++; print "$i - $thread\n"; if (??????? $thread finished ??????) {last}; sleep 1; };
Thanks and !!! HAPPY NEW YEAR !!!

Replies are listed 'Best First'.
Re: Win32 ActiveState Perl 5.8 - use threads
by BrowserUk (Pope) on Jan 03, 2005 at 12:31 UTC
    Whether it is possible to determine that thread is finished not using additional variables through threads::shared

    Yes, with a caveat. You cannot do anything else whilst you wait for it to complete.

    use threads; my $thread = async {sleep 10; print "Ok\n"; return 1 }; my $returnValue = $thread->join; print $thread->tid, ' completed and returned: ', $returnValue, "\n";

    join will wait for it's referrent thread to terminate. However, once you enter that wait state there is no way to regain control until the thread terminates, which means doing anything else whilst you wait is impossible and if the thread hangs, deadlocks or disappears up it's own bum in an endless loop, you can do nothing about it!

    This is one (of several) of the anacronisms of the POSIX pthreads API that Perl's iThreads emulate.

    For this reason, I always detach my threads (throwing away any return value and rendering the join API useless) and arrange my own mechanisms for detecting when a thread is finished.

    I'd prefer not have to do this "roll-my-own" adminstrative code, but the broken API makes it impossible to do otherwise and retain control


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.
Re: Win32 ActiveState Perl 5.8 - use threads
by ambrus (Abbot) on Jan 03, 2005 at 09:36 UTC

    Probably no, at least it's not documented in perldoc threads.

    Install ruby, create a ruby thread, and than you can use the alive? method of a Thread to see if it's finished yet.

    Update: A straightforward translation of your code to ruby is:

    #!ruby -w thread = Thread.new { sleep 10; puts "Ok"; }; i = 0; while true; i += 1; puts "#{i} - #{thread}"; if !thread.alive?; break; end; sleep 1; end; __END__
Re: Win32 ActiveState Perl 5.8 - use threads
by zentara (Archbishop) on Jan 03, 2005 at 14:40 UTC
    Just a simple idea for you...can you put a shared variable in each thread called $im_alive, and set it to 1 when the thread is launched, and have the thread set it to 0 just before it exits. Of course that requires you to track each thread (and it's associated shared variable) in a hash if you have more than one thread going. There also may be timing problems in checking the shared:var, you may need to allow time for the thread to initiate before checking it. I avoided this by setting the shared var to 1 outside of the thread.
    #!/usr/bin/perl use warnings; use strict; use threads; use threads::shared; my $im_alive : shared = 1; my $thread = async {sleep 10; print "Ok\n"; $im_alive = 0; }; my $i = 0; while (1) { $i++; print "$i - $thread\n"; print "im_alive->$im_alive\n"; if ($im_alive == 0) {last}; sleep 1; }; print "im_alive->$im_alive\n"; print "hit enter to exit\n"; <>;

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://418898]
Approved by Courage
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2018-01-24 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How did you see in the new year?










    Results (254 votes). Check out past polls.

    Notices?