From: Boerni on
Hi all

I tried to run the sample script from the "tutoriol on threads in perl" that
should list prime numbers between 1 and 1000.

The Problem is, the script always ends with this errormessage "A thread
exited while 2 threads were running."
The last Prime it finds is 661.
After adding some prints, it looks like if no more threads are started once
120 threads are running.
The thread to check for 661 is created, but never starts.

I use ActiveState Perl 5.8.2 on WinXP.
The script runs fine on Linux (... yeah I know... but I want to use threads
on Win32 boxes :)

Can someone explain this and maybe has a workaround?

I hope this question hasn't been answered before... I googled but couldn't
find the answer for this problem.

Thanx in advance
Boerni

This is the script i try to run:

----- Start Code --------
#!/usr/bin/perl -w
# prime-pthread, courtesy of Tom Christiansen

use strict;

use threads;
use threads::shared;
use Thread::Queue;

$| = 1;

my %running_threads : shared;

my $stream = new Thread::Queue;
my $kid = new threads(\&check_num, $stream, 2);

for my $i ( 3 .. 1000 ) {
$stream->enqueue($i);
}

$stream->enqueue(undef);
$kid->join;

my @running = keys %running_threads;
print "\nStill checking for @running\n";


sub check_num {
my ($upstream, $cur_prime) = @_;
my $kid;
my $downstream = new Thread::Queue;
my $num_to_join;

$running_threads{$cur_prime}++;
print "Thread checking $cur_prime has started\n";

while (my $num = $upstream->dequeue) {
next unless $num % $cur_prime;
if ($kid) {
#print "Thread checking $cur_prime is adding $num to
downstream\n";
$downstream->enqueue($num);
} else {
print "Found prime $num\n";
$kid = new threads(\&check_num, $downstream, $num);

$num_to_join = $num;
print "Thread checking $cur_prime initialized a new thread to
check $num. ThreadID: ".$kid->tid."\n";
#print "Still checking for ". keys( %running_threads)." other
primes\n";

}
}
$downstream->enqueue(undef) if $kid;
$kid->join if $kid;
print "$num_to_join joined by thread checking $cur_prime\n";
delete $running_threads{$cur_prime};
print "Thread checking $cur_prime ended\n";
}
----- End Code --------

----- Some Output -----
.....
.....
Thread checking 647 initialized a new thread to check 653. ThreadID: 119
Thread checking 653 has started
Found prime 659
Thread checking 653 initialized a new thread to check 659. ThreadID: 120
Thread checking 659 has started
Found prime 661
Thread checking 659 initialized a new thread to check 661. ThreadID: 121
661 joined by thread checking 659
Thread checking 659 ended
659 joined by thread checking 653
Thread checking 653 ended
653 joined by thread checking 647
Thread checking 647 ended
.....
.....
5 joined by thread checking 3
Thread checking 3 ended
3 joined by thread checking 2
Thread checking 2 ended

Still checking for 661
A thread exited while 2 threads were running.


From: xhoster on
"Boerni" <alt_test.3.boerni(a)spamgourmet.com> wrote:
> Hi all
>
> I tried to run the sample script from the "tutoriol on threads in perl"
> that should list prime numbers between 1 and 1000.
>
> The Problem is, the script always ends with this errormessage "A thread
> exited while 2 threads were running."
> The last Prime it finds is 661.
> After adding some prints, it looks like if no more threads are started
> once 120 threads are running.


When stress-testing Perl under Windows, I've also found 120 to be about
the limit on the number of simultaneous threads.

> Can someone explain this and maybe has a workaround?

A workaround would probably depend on why you want to have more than 120
threads around in the first place. I think that just wanting to run the
cutesy but impractical code from the tutorial is not a very good enough
reason.

Also, you should check that the creation of new threads succeeds. That
would have removed some of the mystery from the failure.

> my $kid = new threads(\&check_num, $stream, 2)

or die $!;

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
From: egipcia_i on
hi