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

S

Samuel

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
 
S

Samuel

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.
 
X

xhoster

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top