I am trying to pass data between a thread and the rest of my program and it's driving me crazy because I can't get it to work. Of course it's bound to be my lack of knowledge but I've spent days and days trying to get it to work and i'm at my wit's end. I would REALLY appreciate some help. I know the following post is quite long(even tho I tried to keep it quite short) but if you guys could possibly take a look and give me some ideas even it would be a great help.
I am writing a program that takes feeds of financial data and interrogates them. The main part of the program is generalised and does not really know anything about the data it is consuming. You start the program and passes it the name of a "plugin"(implemented as a module") thus :
The Agent then "requires" the plugin(module) :MdAgent.pl --plugin UTP --config UTP --interface ce1 --mcastAddress 2 +39.11.25.104 --mcastPort 104
At this point it used to enter a loop, reading packets and passing them to the sub in the plugin that understood them :require "$PALIBDIR/$actualPlugin.pm"; <c/> It then calls the initialisation sub of the plugin : <c> $actualPlugin->moduleInit($debug, $instance);
now originally in the processMessage sub of the plugin I would update a hash of hashes in the main package :while(1) { $drop=1 unless $peer->recv($sock, $message, $maxLen,0) if($drop ==1) { $DEBUG && msg("recv failed : $!"); } else { $actualPlugin->processMessage("$message"); }
...that was before I made the code multithreaded for performance reasons. Now, I am creating a thread that reads the packets, and then calls the processMessage sub of the plugin :$MdAgent::TotalsHsh{$service}{$messageType}+=1;
this thread is the thing that now calls the processMessage sub.$mcastReaderThread=threads->create(\&doMcastSubscription, $interface, +$mcastAddress, $mcastPort); $mcastReaderThread->detach();
I'm aware that the area of memory used by the thread is different to the one used by the main part of the program, but I need to communicate across the boundary.
As far as I am aware I cannot used threads:shared since a hash of hashes is too complex. Now I thought I could use the old Threads model and communicate across the boundary using the same method as I did before making the program multithreaded, ie. :
$MdAgent::TotalsHsh{$service}{$messageType}+=1;
However that doesn't work(I'm assuming because the thread still has it's own seperate memory area, even though I though this would work with the old Thread model)
I've also tried creating a reference to the hash of hashes and passing it into the $mcastReaderThread thread, and then having that thread pass it into the plugin's processMessage sub, but that doesn't work either, I'm sure because of the same reasons above.I think I could use queues but I don't want to do that since enqueuing and dequeuing will be quite an overhead when in essence all I want to do is update a counter using the thread and have it visible to the main part of the progam.
Help : (In reply to communication across threads using hash of hashes by stevehicks
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |