#! 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.
|