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

Discussion in 'Perl Misc' started by Samuel, May 8, 2007.

  1. Samuel

    Samuel Guest

    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
    Samuel, May 8, 2007
    #1
    1. Advertising

  2. Samuel

    Samuel Guest

    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.
    Samuel, May 8, 2007
    #2
    1. Advertising

  3. Samuel

    Guest

    Samuel <> 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
    , May 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Sway
    Replies:
    1
    Views:
    388
    William Ryan
    Aug 30, 2003
  2. Ben Fidge
    Replies:
    1
    Views:
    407
    Patrick Steele [MVP]
    Jun 22, 2004
  3. David Hirschfield
    Replies:
    3
    Views:
    3,287
    Thomas Guettler
    Feb 3, 2006
  4. Lars =?iso-8859-15?Q?Gust=E4bel?=

    Re: Best way to determine if a certain PID is still running

    Lars =?iso-8859-15?Q?Gust=E4bel?=, Feb 3, 2006, in forum: Python
    Replies:
    2
    Views:
    450
    MrJean1
    Feb 4, 2006
  5. Eric Promislow
    Replies:
    5
    Views:
    111
    Lloyd Linklater
    Sep 4, 2008
Loading...

Share This Page