Threads don't seem to work as advertised

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
... 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):

while (1)
   Time::HiRes::ualarm($secs, 0);
   my $thr=threads->create('dothread');
   sleep 100;
   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:
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
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
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...


Re: Threads don't seem to work as advertised

Quoted text here. Click to load it

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.


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]

Site Timeline