You have (at least) 3 options:
Ask for help when you run into problems.
#! perl -slw use strict; use threads; use Thread::Queue; my $n = 100; sub getWorkItems { ## return next workitem ## return $n--; } sub worker { my $tid = threads->tid; my( $Qwork, $Qresults ) = @_; while( my $work = $Qwork->dequeue ) { my $result; ## Process $work to produce $result ## $result = "$tid : result for workitem $work"; $Qresults->enqueue( $result ); } $Qresults->enqueue( undef ); ## Signal this thread is finished } our $THREADS = 10; my $Qwork = new Thread::Queue; my $Qresults = new Thread::Queue; ## Create the pool of workers my @pool = map{ threads->create( \&worker, $Qwork, $Qresults ) } 1 .. $THREADS; ## Get the work items (from somewhere) ## and queue them up for the workers while( my $workItem = getWorkItems() ) { $Qwork->enqueue( $workItem ); } ## Tell the workers there are no more work items $Qwork->enqueue( (undef) x $THREADS ); ## Process the results as they become available ## until all the workers say they are finished. for ( 1 .. $THREADS ) { while( my $result = $Qresults->dequeue ) { ## Do something with the result ## print $result; } } ## Clean up the threads $_->join for @pool;
In reply to Re: How to create thread pool of ithreads
by BrowserUk
in thread How to create thread pool of ithreads
by elf_firein
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |