Threads don't seem to work as advertised

Discussion in 'Perl Misc' started by Mark Seger, Jul 3, 2008.

  1. Mark Seger

    Mark Seger Guest

    .... but then again it something silly I'm doing.

    I'm trying a variety of the basic and thing like creating threads works
    just fine - in fact that's what I used to test my implementation of
    monitoring procosses by thread in collectl and so I then used collectl
    to test my implementation when things didn't look quite right.

    Specifically, the is-running() method doesn't appear to be there because
    when I try to execute it perl tells me it can't find it. this is a
    standard RHEL4.2 release and perl 5.8.5. But I think I may be able to
    live with that if I can get other things to work.

    First a couple of words about what I'm doing. I have a main script that
    sits in an infinite loop, sets an alarm for 5 seconds, fires off a
    thread which itself sleeps for 1 second and exits. In other words
    (leaving off the sigalarm routine and some intermediate print statements):

    $secs=5*1000000;
    while (1)
    {
    Time::HiRes::ualarm($secs, 0);
    my $thr=threads->create('dothread');
    sleep 100;
    @running=threads::list(threads::running);
    printf "Awake at: %s State: %d\n", getSecs(), scalar(@running);
    }

    sub dothread
    {
    my $secs=1000000;
    sleep 2;
    }

    This works just fine, but what I really want to be able to do in the
    mainline is figure out if my thread hung. The documentation says that
    threads::list(threads::running) will return a list of the running
    threads. So I did the following:
    @running=threads::list(threads::running);
    printf "Running %d\n", scalar(@running);
    right after my main timer when off in the main loop and not only was the
    size of @running not zero like I expected it to be since there weren't
    any running threads, every time I called it it incremented by 1. How
    can that be if there are no threads running? I even tried an undef of
    @results right before setting it just to be sure and it keeps growing.

    To make sure there weren't any other threads running I ran collectl
    during the test and you can even see threads coming and going as from
    the following:

    # PROCESS SUMMARY (faults are /sec)
    # PID User PR PPID S VSZ RSS CP SysT UsrT Pct
    AccuTime MajF MinF Command
    21:00:55 14681 root 16 6533 S 119M 7M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl
    21:00:56 14681 root 16 6533 S 130M 7M 2 0.00 0.00 0
    0:00.03 0 175 /usr/bin/perl
    21:00:56 14723+ root 17 14681 S 130M 7M 2 0.00 0.00 0
    0:00.00 0 1 ipmi-thread.pl
    21:00:57 14681 root 16 6533 S 130M 7M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl
    21:00:58 14681 root 16 6533 S 130M 7M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl
    21:00:59 14681 root 16 6533 S 130M 7M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl
    21:01:00 14681 root 16 6533 S 130M 7M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl
    21:01:01 14681 root 16 6533 S 141M 8M 2 0.00 0.00 0
    0:00.03 0 175 /usr/bin/perl
    21:01:01 14724+ root 17 14681 S 141M 8M 2 0.00 0.00 0
    0:00.00 0 1 ipmi-thread.pl
    21:01:02 14681 root 16 6533 S 141M 8M 2 0.00 0.00 0
    0:00.03 0 0 /usr/bin/perl

    My main process is pid 14681 and you can see it create a thread with a
    tid of 14723 which I call a pid in collectl to keep the display neater).
    After a second it goes away like it should and 5 seconds later a new
    thread, 14724 starts up and a second later it exits.

    So why does threads::list(threads::running) return a growing array.

    Then I had another thought. Since I can get the tid of a thread maybe I
    can see if that process still exists so I looked at '$thr->tid()', but
    instead of returning a tid like it says, it simply returns an
    incrementing number starting at 1 which does me no good trying to map it
    to a real tid. Should I be able to get the real tid of a thread?

    Finally, as one piece of good news, if I set an alarm within the thread,
    it does appear to wake me up when it should without interfering with the
    alarm being delivered to the main line code so perhaps I have a
    mechanism I can work with after all - I'm basically trying to execute a
    command in the thread and figure out if it hangs.

    But never-the-less, if I'm doing something wrong with the threads
    package I'd sure like to know what it is.

    sorry for being so long winded...

    -mark
    Mark Seger, Jul 3, 2008
    #1
    1. Advertising

  2. Mark Seger

    Ben Morrow Guest

    Quoth Mark Seger <>:
    > ... but then again it something silly I'm doing.
    >
    > I'm trying a variety of the basic and thing like creating threads works
    > just fine - in fact that's what I used to test my implementation of
    > monitoring procosses by thread in collectl and so I then used collectl
    > to test my implementation when things didn't look quite right.
    >
    > Specifically, the is-running() method doesn't appear to be there because
    > when I try to execute it perl tells me it can't find it. this is a
    > standard RHEL4.2 release and perl 5.8.5. But I think I may be able to
    > live with that if I can get other things to work.
    >
    > First a couple of words about what I'm doing. I have a main script that
    > sits in an infinite loop, sets an alarm for 5 seconds, fires off a
    > thread which itself sleeps for 1 second and exits. In other words
    > (leaving off the sigalarm routine and some intermediate print statements):
    >
    > $secs=5*1000000;
    > while (1)
    > {
    > Time::HiRes::ualarm($secs, 0);
    > my $thr=threads->create('dothread');
    > sleep 100;
    > @running=threads::list(threads::running);
    > printf "Awake at: %s State: %d\n", getSecs(), scalar(@running);
    > }
    >
    > sub dothread
    > {
    > my $secs=1000000;
    > sleep 2;
    > }
    >
    > This works just fine, but what I really want to be able to do in the
    > mainline is figure out if my thread hung. The documentation says that
    > threads::list(threads::running) will return a list of the running
    > threads. So I did the following:
    > @running=threads::list(threads::running);
    > printf "Running %d\n", scalar(@running);
    > right after my main timer when off in the main loop and not only was the
    > size of @running not zero like I expected it to be since there weren't
    > any running threads, every time I called it it incremented by 1. How
    > can that be if there are no threads running? I even tried an undef of
    > @results right before setting it just to be sure and it keeps growing.


    Are you reading your local copy of the documentation, or a copy on the
    web? If you're reading a non-local copy, you may need to upgrade your
    threads: is_running and threads::running were introduced in 1.34,
    whereas for instance 5.8.8 came with 1.07.

    Ben

    --
    It will be seen that the Erwhonians are a meek and long-suffering people,
    easily led by the nose, and quick to offer up common sense at the shrine of
    logic, when a philosopher convinces them that their institutions are not based
    on the strictest morality. [Samuel Butler, paraphrased]
    Ben Morrow, Jul 4, 2008
    #2
    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. Fritz
    Replies:
    2
    Views:
    5,303
    Richard Wheeldon
    Mar 25, 2005
  2. Victor Bazarov
    Replies:
    2
    Views:
    392
    red floyd
    Aug 1, 2003
  3. Stuart Brockman
    Replies:
    3
    Views:
    312
    Bo Persson
    Oct 23, 2008
  4. Brian D
    Replies:
    4
    Views:
    865
    Brian D
    Jan 6, 2010
  5. omar

    DPAPI User Store Does Not Work as advertised

    omar, Nov 17, 2004, in forum: ASP .Net Security
    Replies:
    6
    Views:
    202
    Patricio Jutard
    Nov 20, 2004
Loading...

Share This Page