use strict; use warnings; use threads qw(yield); use threads::shared; use Data::Dumper; sub test { my %h : shared; for (1..3) { threads->new(sub { my($n) = @_; lock(%h); $h{$n} = rand(); threads->yield(); }, $_)->join(); } return %h; } my %k = test(); print Dumper(\%k); #### $VAR1 = { '1' => undef, '3' => undef, '2' => undef }; #### use strict; use warnings; use threads qw(yield); use threads::shared; use Data::Dumper; my %h : shared; sub test { for (1..3) { threads->new(sub { my($n) = @_; lock(%h); $h{$n} = rand(); threads->yield(); }, $_)->join(); } return %h; } my %k = test(); print Dumper(\%k); # output $VAR1 = { '1' => '0.608570207216875', '3' => '0.0436435554447634', '2' => '0.220397240555943' }; #### use strict; use warnings; use threads qw(yield); use threads::shared; use Data::Dumper; sub test { my %h : shared; for (1..3) { threads->new(sub { my($n) = @_; lock(%h); $h{$n} = rand(); threads->yield(); }, $_)->join(); } return \%h; } my $k = test(); print Dumper($k); # output $VAR1 = { '1' => '0.653087966217758', '3' => '0.0419468023619665', '2' => '0.199076903824793' };