J
jl_post
Hi,
I recently read the "perldoc perlthrtut" document to learn how to
do threading with Perl, and I understand most of what it's teaching.
However, I have a question regarding the complete example given (the
one about generating prime numbers using a pipeline model):
#!/usr/bin/perl
# prime-pthread, courtesy of Tom Christiansen
use strict;
use warnings;
use threads;
use Thread::Queue;
my $stream = Thread::Queue->new();
for my $i ( 3 .. 1000 ) {
$stream->enqueue($i);
}
$stream->enqueue(undef);
threads->create(\&check_num, $stream, 2)->join();
sub check_num {
my ($upstream, $cur_prime) = @_;
my $kid;
my $downstream = Thread::Queue->new();
while (my $num = $upstream->dequeue()) {
next unless ($num % $cur_prime);
if ($kid) {
$downstream->enqueue($num);
} else {
print("Found prime $num\n");
$kid = threads->create(\&check_num, $downstream, $num);
}
}
if ($kid) {
$downstream->enqueue(undef);
$kid->join();
}
}
(For the record, I deleted the first "$kid->join();" line (the one
that was right after the first "thread->create" call) and appended "-
If I understand correctly, each thread takes a queue of numbers,
reports that the first is prime (otherwise it wouldn't be first in the
queue), and then starts a new thread. The old thread then gives the
new thread all the numbers in its queue that are not multiples of the
prime number it reported.
I ran the script, and it works great. However, it seems that a thread
won't end until its child thread (that is, the thread it created/
started) ends. And since it seems like it creates another thread for
every prime number, that would mean that around 167 different threads
can be running (or at least exist) at once. (I say 167 because there
are 167 primes from 3 to 1000.)
So am I correct in saying that there are over 100 threads running at
once? If so, isn't that a bit taxing on the operating system? I
mean, I've noticed that in some cases over 100 levels of recursion can
error out (or at least give a warning message), so wouldn't
recursively creating a thread over 100 times also be something we'd
want to avoid? Or are threads different in this respect?
Thanks in advance for any advice.
-- Jean-Luc
I recently read the "perldoc perlthrtut" document to learn how to
do threading with Perl, and I understand most of what it's teaching.
However, I have a question regarding the complete example given (the
one about generating prime numbers using a pipeline model):
#!/usr/bin/perl
# prime-pthread, courtesy of Tom Christiansen
use strict;
use warnings;
use threads;
use Thread::Queue;
my $stream = Thread::Queue->new();
for my $i ( 3 .. 1000 ) {
$stream->enqueue($i);
}
$stream->enqueue(undef);
threads->create(\&check_num, $stream, 2)->join();
sub check_num {
my ($upstream, $cur_prime) = @_;
my $kid;
my $downstream = Thread::Queue->new();
while (my $num = $upstream->dequeue()) {
next unless ($num % $cur_prime);
if ($kid) {
$downstream->enqueue($num);
} else {
print("Found prime $num\n");
$kid = threads->create(\&check_num, $downstream, $num);
}
}
if ($kid) {
$downstream->enqueue(undef);
$kid->join();
}
}
(For the record, I deleted the first "$kid->join();" line (the one
that was right after the first "thread->create" call) and appended "-
join()" to the previous line to make the program compile.)
If I understand correctly, each thread takes a queue of numbers,
reports that the first is prime (otherwise it wouldn't be first in the
queue), and then starts a new thread. The old thread then gives the
new thread all the numbers in its queue that are not multiples of the
prime number it reported.
I ran the script, and it works great. However, it seems that a thread
won't end until its child thread (that is, the thread it created/
started) ends. And since it seems like it creates another thread for
every prime number, that would mean that around 167 different threads
can be running (or at least exist) at once. (I say 167 because there
are 167 primes from 3 to 1000.)
So am I correct in saying that there are over 100 threads running at
once? If so, isn't that a bit taxing on the operating system? I
mean, I've noticed that in some cases over 100 levels of recursion can
error out (or at least give a warning message), so wouldn't
recursively creating a thread over 100 times also be something we'd
want to avoid? Or are threads different in this respect?
Thanks in advance for any advice.
-- Jean-Luc