Hi
I am writing a small app that listens to UDP datagrams sent to it from different sources and matches them up. It has a couple of threads between which it needs to share hash elements, however, I can't seem to update the hashes properly.
- the main thread listens to the messages from 3 different sources. For each mesaage it creates an entry in a dedicated hash for the given source.
- the second thread wakes up and is supposed to look at the three dedicated hashes and match up messages from each.
I am only dealing with the messages from one of these sources for now, until I can get it working.
In "main" I declare the hash for the "orc" messages and assign a "dummy" element to it that consists of a key, and a value that is another hash :
my %orcPriceHsh:shared=1; my %signatureHsh:shared = ("DE00084695946" => "0000000000.000000"); $orcPriceHsh{"0000000000"} = \%signatureHsh;
I then call the sub that listens to the messages across the network :
handleMessages($configHsh{General}{ListenPort},%orcPriceHsh);
...and spawn a thread that is supposed to look at the nested hash :
$transitCalcThread=threads->new(\&calcTransitTimes, $XmlRpcClient,$itr +sQueue,%orcPriceHsh); $transitCalcThread->detach;
in handleMessages() I assign the passed nested hash to a var "%oph" :
sub handleMessages { my $port=shift(); my %oph=@_;
...and whenever I "hear" an "orc" message I create a shared hash and add it to the top-level hash : (signature, is for example "DE0008469594621896219.525, and timeStmap is, for example "1282132286.326053", nixSeconds is, for example "1282132286,")
my %sHsh:shared = ($signature => $timeStamp); $oph{$nixSeconds} = \%sHsh;
I can dump the nested hash inside this function ok :
msg("in handleMessages() oph :"); dump(%oph);
gives :
12:51:32 : dump of oph in handleMessages() : ( 1282132292, { # tied threads::shared::tie "DE0008469594621896219.526" => "1282132292.211252", }, 1282132287, { # tied threads::shared::tie "DE00084695946218362192" => "1282132287.43417", }, 1282132286, { # tied threads::shared::tie "DE00084695946218662192" => "1282132286.326053", }, 1, undef, 0, { # tied threads::shared::tie DE00084695946 => "0000000000.000000", }, 1282132290, { # tied threads::shared::tie "DE00084695946218362192" => "1282132290.969117", }, )
but even tho the thread running calcTransitTimes() can see the initial "dummy" element it cannot see the others. This code in said function :
sub calcTransitTimes { my $XmlRpcClient=shift(); my $itrsQueue=shift(); #my %orcPriceHsh=shift(); my %args=@_; ... msg("in calcTransitTimes() args :"); dump(%args);
gives me :
12:51:32 : in calcTransitTimes() args : ( 1, undef, 0, { # tied threads::shared::tie DE00084695946 => "0000000000.000000", }, )
I know there is probably a very simple answer but I have burned a load of time on this already and I have a rapidly approaching deadline to meet. Any help would be greatly appreciated : * }
In reply to Help !! problems sharing a nested hash across threads by stevehicks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |