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.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^4: Parallel Processing in Perl
by Anonymous Monk on Mar 29, 2010 at 05:46 UTC
    Hi BrowserUk,
    Yes It worked for me and its fulfilled my requirements;

    -Thanks a lot
    Shree
Re^4: Parallel Processing in Perl
by shree (Acolyte) on Mar 29, 2010 at 05:53 UTC
    Hi BrowserUk,

    Thanks for your quick and kind response. Yes it totally fulfilled my requirement.

    -Thanks a lot...
    Shree