Hi

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 :

MdAgent.pl --plugin UTP --config UTP --interface ce1 --mcastAddress 2 +39.11.25.104 --mcastPort 104
The Agent then "requires" the plugin(module) :
require "$PALIBDIR/$actualPlugin.pm"; <c/> It then calls the initialisation sub of the plugin : <c> $actualPlugin->moduleInit($debug, $instance);
At this point it used to enter a loop, reading packets and passing them to the sub in the plugin that understood them :
while(1) { $drop=1 unless $peer->recv($sock, $message, $maxLen,0) if($drop ==1) { $DEBUG && msg("recv failed : $!"); } else { $actualPlugin->processMessage("$message"); }
now originally in the processMessage sub of the plugin I would update a hash of hashes in the main package :
$MdAgent::TotalsHsh{$service}{$messageType}+=1;
...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 :
$mcastReaderThread=threads->create(\&doMcastSubscription, $interface, +$mcastAddress, $mcastPort); $mcastReaderThread->detach();
this thread is the thing that now calls the processMessage sub.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.