in reply to Re: communication across threads using hash of hashes
in thread communication across threads using hash of hashes
Thanks very much for your help guys. I'vd spent a while messing around with this and it does appear that it is because of the seperate package. To make things simpler I've used a scalar. This is the scene :
I declare the shared var in the main program :I then "require" at rumtime the plugin name(which equates to a module) that was passed as a parameter to the program :use threads; use threads::shared; my $sharedTestVar :shared;
I then assign a value to the test scalar :... if(! GetOptions ('plugin=s' => \$plugin,'config=s' => \$instanceName,' +debug' => \$debug, 'interface=s' => \$interface, 'mcastAddress=s' => +\$mcastAddress, 'mcastPort=s' => \$mcastPort, 'file=s' => \$snoopf, ' +remotePcapForwarder=s' => \$remotePCF, 'netprobe=s' => \$netprobe)) { usage(); xit("incorrect options specification", 99); } if($debug) { msg("info : turning on debug"); $ENV{TRACE}=1; $DEBUG=1; } if(! $plugin) { usage(); xit("you must supply a plugin name", 98); } msg("info : plugin \"$plugin\" will be connected"); eval (require "$PALIBDIR/$plugin.pm");
I then create a thread and detach it :$sharedTestVar=6; $DEBUG && msg("init : set sharedTestVar == $sharedTestVar");
In the doMcastSubscription sub I increment the scalar :$mcastReaderThread=threads->new(\&doMcastSubscription, $interface,$mca +stAddress,$mcastPort); $mcastReaderThread->detach;
meanwhile I look at the scalar in the main loop of the program :sub doMcastSubscription { ... while(1) { my($message,$peer); $drop=1 unless $peer=recv($sock,$message,1400,0); if($drop == 1) { $DEBUG && msg("recv failed"); #sleep(2); } else { $actualPlugin->processMessage("$message"); } $sharedTestVar++; msg("in doMcastSubscription set sharedTestVar == $sharedTestVa +r"); $drop=0; } $DEBUG && msg("leaving doMcastSubscription()"); }
When I do the above, it works fine, i.e the thread updates the shared scalar with no issue. However, when I move the code that increments the scalar to the file that I "required" at runtime it doesn't work. Here's what the thread is doing now :while(1) { ... msg("in main while loop, sharedTestVar == $sharedTestVar"); ... sleep(5); }
and here's the processMessage sub in UTP.pm :sub doMcastSubscription { ... while(1) { my($message,$peer); $drop=1 unless $peer=recv($sock,$message,1400,0); if($drop == 1) { $DEBUG && msg("recv failed"); #sleep(2); } else { $actualPlugin->processMessage("$message"); } $drop=0; } }
sub processMessage { ... if($FeedAgent::sendRateStats) { $sharedTestVar++; msg("in processMessage set sharedTestVar == $sharedTestVar"); + } ... }
I've tried declaring the shared scalar with "our" instead of "my" but this doesn't help either and not not sure what else to do as this is way past my understanding : * (
Any ideas ?? My only thought for a workaround is to use shared memory but I'd rather notOnce again thanks for trying to help, I really appreciate it !!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: communication across threads using hash of hashes
by stevehicks (Sexton) on Dec 21, 2009 at 11:20 UTC | |
by zentara (Cardinal) on Dec 21, 2009 at 12:21 UTC |