use strict; use threads; use threads::shared; my %hash = ( 'a' => 'foo', 'b' => 'foo', 'c' => 'foo', 'd' => 'foo' ); share($hash{$_}) foreach('a'..'f'); my $tid = 0; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } my $th1 = async { $tid = 1; print "thread $tid started\n"; sleep 2; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } print "delete b from thread $tid\n"; delete $hash{'b'}; print "undefine d from thread $tid\n"; undef $hash{'d'}; print "setting f => foo in thread $tid\n"; $hash{'f'} = 'foo'; }; my $th2 = async { $tid = 2; print "thread $tid started\n"; sleep 3; foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } } }; sleep 1; print "delete a from thread $tid\n"; delete $hash{'a'}; print "undefine c from thread $tid\n"; undef $hash{'c'}; print "setting e => foo in thread $tid\n"; $hash{'e'} = 'foo'; $th1->join(); $th2->join(); foreach my $key('a'..'f') { if(exists $hash{$key}) { if(defined $hash{$key}) { print "$key => $hash{$key} in thread $tid\n"; } else { print "$key => undef in thread $tid\n"; } } else { print "$key does not exist in thread $tid\n"; } }