Your code does indeed work but if I try to use the same methodology in my code it doesn't. I get :
...and then after that I create the thread :Thread 1 terminated abnormally: lock can only be used on shared values + at UTP.pm line 189 <c> Do you think this is because of the separate package ?? I declare and initialise the hash in MdAgent.pl : <c> ... my %Totals :shared; ... msg("initialising totals"); foreach my $envo(sort keys %{$marketMsgHsh{$plugin}{environments}}) { $DEBUG && msg("sharing $envo"); $Totals{$envo}=&share( {} ); $DEBUG && msg("done."); foreach my $messageType(sort keys %{$marketMsgHsh{$plugin}{message +Types}}) { $Totals{$envo}{$messageType}=0; } } msg("done."); ...
in the thread I call the sub(in the "UTP" package)that actually handles the messages :... $mcastReaderThread=threads->create(\&doMcastSubscription, $interface, +$mcastAddress, $mcastPort); $mcastReaderThread->detach(); ...
processMessage is defined in "UTP.pm". this is what I have it doing right now :... while(1) { $drop=1 unless $peer->recv($sock, $message, $maxLen,0) if($drop ==1) { $DEBUG && msg("recv failed : $!"); } else { $actualPlugin->processMessage("$message"); } ...
Is this going wrong because the processMessage sub is in a different package or am I doing something else wrong ??... if($FeedAgent::sendRateStats) { lock %{$Totals{$serviceID}}; $Totals{$serviceID}{$packetType}++; $DEBUG && msg("set Totals{$serviceID}{$packetType} == $Totals{ +$serviceID}{$packetType}");; } ...
In reply to Re^2: communication across threads using hash of hashes
by stevehicks
in thread communication across threads using hash of hashes
by stevehicks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |