in reply to Is this a job for a fork?
As BrowserUk said, threads may be an easier choice. I noodled around with this before I saw his reply and came up with this adaptation of your script.
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; use threads; use threads::shared; my %count : shared; my $maxthreads = 3; # there are way more lines than this but anyway... for my $i ( 1 .. 500 ) { redo if ( scalar threads->list() >= $maxthreads ); # this just represents that I've found a pair and want to run the +sub if ( $i % 50 == 0 ) { # run the sub my $thread = threads->new( \&inc, $i, 2 ); } } while ( scalar threads->list() ) { sleep 1 } # I need access to the hash that's updated in the sub print Dumper \%count; sub inc { my ( $param1, $param2 ) = @_; #long subroutine using passed params # pausing for effect! # this simulates that the sub takes a while to run sleep( 5 + ( rand 5 ) ); # let you know something is happening print "$param1\n"; # make a change to the hash $count{$param1} = $param1 * $param2; threads->self()->detach; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Is this a job for a fork?
by richardwfrancis (Beadle) on Jul 13, 2010 at 09:14 UTC | |
by BrowserUk (Patriarch) on Jul 13, 2010 at 09:47 UTC | |
by richardwfrancis (Beadle) on Jul 13, 2010 at 12:37 UTC | |
by richardwfrancis (Beadle) on Jul 13, 2010 at 13:52 UTC | |
by thundergnat (Deacon) on Jul 13, 2010 at 20:00 UTC |