in reply to Re^2: Parallel Processing in Perl
in thread Parallel Processing in Perl
There are several ways of achieving your goal. I cannot infer which is appropriate from your sample code.
Here are two ways:
#! perl -sw use strict; use threads; use threads::shared; use Data::Dumper; my %hash1 :shared; my %hash2 :shared; while(1) { async( \&sub1)->detach; async( \&sub2)->detach; sleep(2); lock %hash1; lock %hash2; print '$hash1'. Dumper(\%hash1) ."\n"; print '$hash2'. Dumper(\%hash2) ."\n"; print "_______________________________\n"; } sub sub1 { my $now = scalar localtime; lock %hash1; $hash1{'time'} = $now; } sub sub2 { my $now = scalar localtime; lock %hash2; $hash2{'time'} = $now; }
#! perl -sw use strict; use threads; use Data::Dumper; my %hash1; my %hash2; while(1) { my( $t1 ) = threads->create( \&sub1 ); my( $t2 ) = threads->create( \&sub2 ); sleep(2); %hash1 = $t1->join; %hash2 = $t2->join; print '$hash1'. Dumper(\%hash1) ."\n"; print '$hash2'. Dumper(\%hash2) ."\n"; print "_______________________________\n"; } sub sub1 { my $now = scalar localtime; return time => $now; } sub sub2 { my $now = scalar localtime; return time => $now; }
Try them, note the differences, and ask questions.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Parallel Processing in Perl
by Anonymous Monk on Mar 29, 2010 at 05:46 UTC | |
|
Re^4: Parallel Processing in Perl
by shree (Acolyte) on Mar 29, 2010 at 05:53 UTC |