Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Determine whether a thread is still running in Perl 5.8.8 with "interpreter threads"

Reply
Thread Tools

Determine whether a thread is still running in Perl 5.8.8 with "interpreter threads"

 
 
Samuel
Guest
Posts: n/a
 
      05-08-2007
Given the following snippet:

-----------------------
#!/usr/bin/env perl
use threads;

sub threadfunc { my $i = 0; while ($i < 3) { shift; print; $| = 1; $i+
+; sleep(1); } }

my $thread1 = threads->create(\&threadfunc, '.');
my $thread2 = threads->create(\&threadfunc, '#');
print "Threads created.\n";

while (threads->list() > 0) {
for my $thread (threads->list()) {
next unless $thread->running(); # how can this be done?
print "Joining thread: $thread (" . $thread->tid() . ")\n";
$thread->join();
}
print "Waiting...\n";
sleep 1;
}
print "Done.\n";
-----------------------

How can I check whether a thread is still running? Note the comment in
the code at "next unless $thread->running();".

I realize that this is not necessary in this situation, but this is
only a test case. Any hints?

-Samuel

 
Reply With Quote
 
 
 
 
Samuel
Guest
Posts: n/a
 
      05-08-2007
Thanks, Chris. I have been reluctant to do this as this as it won't
catch if a thread fails unexpectedly before marking itself finished,
but I suppose there is not much else to do (except upgrade to a later
version of Perl).

Thanks again for your help.

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      05-10-2007
Samuel <(E-Mail Removed)> wrote:
....

> How can I check whether a thread is still running? Note the comment in
> the code at "next unless $thread->running();".


Have you tried Thread::State or Thread::Running? I don't think I
understand the significance of 5.8.8 in your subject. Do you have a
solution that works with a newer (or older) perl?

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a Ruby program determine whether it's running on a 32-bit or64-bit system? Eric Promislow Ruby 5 09-04-2008 01:27 PM
Re: Best way to determine if a certain PID is still running Lars =?iso-8859-15?Q?Gust=E4bel?= Python 2 02-04-2006 01:04 AM
Best way to determine if a certain PID is still running David Hirschfield Python 3 02-03-2006 03:32 PM
Web UserControl - how to determine whether running in IDE Ben Fidge ASP .Net 1 06-22-2004 04:41 PM
Determine whether a query returns results or not (before running it) John Sway ASP .Net 1 08-30-2003 06:31 PM



Advertisments