At 16:43 -0500 11/25/02, Jesse Butler - Sun Microsystems wrote:
Is this list active? If so, I'm not seeing any traffic on it except for what I sent... maybe someone could reply directly to me?
This list isn't very active, but there are a few good people lurking. And some of them answer now and then.. ;-)


Anyhow, I've got my previous question figured out; the threads were going out of scope apparently. If I push each thread onto an array, this code works. Is this correct?
use threads;
Actually, I think you have found a bug. It seems to be some weird interaction between thread objects as returned by threads->list and using "return" inside the thread. I've reduced your problem to the following example:

=====================================================
use threads;

threads->new( sub { 41 } );
print STDOUT (threads->list)[0]->join;

threads->new( sub { return 42 } );
print STDOUT (threads->list)[0]->join; # Doesn't print anything

$t = threads->new( sub { 43 } );
print $t->join;

$t = threads->new( sub { return 44 } );
print $t->join;

print threads->new( sub { 45 } )->join;

print threads->new( sub { return 46 } )->join;
=====================================================

which you think should print "414243444546", but in fact prints "4143444546" (note the missing 42). The difference between 41 and 42 is the use of "return" inside the thread's subroutine.

I will file a perlbug for this. Meanwhile, it would seem better if you save the thread object as returned by "new" (or "create") yourself and better not depend on objects returned by threads->list.


So to go back to your original example, code it like this:

=====================================================
use threads;

push @threads,threads->create(sub { sleep $i; return time(); })
while ($i++ < 5);

foreach $thr (@threads) {
push @results, $thr->join;
}
die "results undefined\n" unless (defined @results);

print $_, "\n" for (@results);
=====================================================

Two final notes:

- if you're running from the main thread, you never need to check whether the object returned by threads->list happens to be threads->self: threads->list only returns thread objects for threads that have _not_ been detached. The main thread (thread #0) is _always_ detached.

- checking for defined( @array ) is deprecated in 5.8. You would know if you would be running with warnings. Which is always highly recommended!



Hope this helps.


Liz


Reply via email to