Next 2.2 Example, Primes using threads Prev

Example, Primes using threads

The same program using threads in the pipeline fashion:

use threads; use Thread::Queue; 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; sub check_num { my ($upstream, $cur_prime) = @_; my $kid = undef; my $downstream = new Thread::Queue; while (my $num = $upstream->dequeue) { next unless $num % $cur_prime; if ($kid) { $downstream->enqueue($num); } else { print "Found prime $num\n"; $kid = new threads(\&check_num, $downstream, $num); } } $downstream->enqueue(undef) if $kid; $kid->join if $kid; }

Next Perl Threads Prev