sub get_data { use threads; use threads::shared; my %result:shared; my $cnt:shared = 10; my $page_counter = sub { my $nb = shift; while ($cnt > 0) { my $n = $cnt; print "Thread # $nb treats string number $n\n"; my $data = 'запрос к серверу'; my @tmp = @{$data}; foreach (my $i = 0; $i <= $#tmp; $i++) { # проблемное место, хочу писать в расшаренный хеш %result = (%result, $tmp[$i] -> {'id'} => shared_clone([$tmp[$i] -> {'name1'}, $tmp[$i] -> {'name2'}])); } $cnt--; } }; for my $t (1..10) { threads -> create($page_counter, $t); } foreach my $t (threads -> list) { if ($t -> tid && !threads::equal($t, threads -> self)) { $t -> join(); } } return \%result; } I want to collect data in a hash from several streams, I do not understand how this is done correctly. Mana read until it reaches. Sample code above.
UPD . Added shared_clone, the result satisfies me.
%result = ()create a completely new hash, it is very slow and I think it knocks the sharing. add one$result{ $tmp[$i] -> {'id'} } = [...]element. About locks.stackoverflow.com/questions/586880/… - Mike$result{ $_->{id} } = [ $_->{name1}, $_->{name2} ] for @{$data};- Mike