in reply to How to create thread pool of ithreads
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;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to create thread pool of ithreads
by Anonymous Monk on Jan 14, 2009 at 13:38 UTC | |
by elf_firein09 (Initiate) on Jan 14, 2009 at 16:52 UTC | |
by BrowserUk (Patriarch) on Jan 14, 2009 at 17:30 UTC | |
by elf_firein (Acolyte) on Jan 28, 2009 at 08:15 UTC | |
by BrowserUk (Patriarch) on Jan 28, 2009 at 08:47 UTC | |
by elf_firein (Acolyte) on Jan 29, 2009 at 14:47 UTC | |
by BrowserUk (Patriarch) on Feb 02, 2009 at 07:50 UTC |