What is the difference between a thread updating a share hash and exiting (A) and a thread joining the main process with return data (B).

Um. In generic terms I can only give you a kind of wishy washy answer. The needs of the application.

For some applications you have the need (or opportunity) at some point in the code two parallelise two pieces of code, but then need the results from both before being able to move on to the next part of the problem.

For example, in your scenario above, it may be that the steps 3) http check; and 4) the authorisation; can be run in parallel, but you need the answers from both before you can continue. So

sub checkMAC { my $mac = shift; my $httpThr = async { return head "http://$mac:80"; ## Simplified!! }; my $authResult = get 'http://AuthServer/Check.pl?$mac'; ## Simplif +ied!! my $httpReult = httpThr->join; ## join immediately lock %macs; $macs{ $mac } = $httpResult && $authResult ? 1 : 0; }

But your log reading thread has no interest in the threads it starts, so it can just detach and forget them.

while( <LOG> ) { next unless m[ ACK \s+ ( (?: [0-9a-f]{2} : ){5} : [0-9a-f]{2} )]xi +; async( \&checkMAB, $1 )->detach; ## detach cos there's no result w +e're interested in }

Still the bit I am unsure about is your step 5). You say: "monitor all MACs and delete them based on certain conditions based on their network activity"

There is (could be?) a conflict between the two bits I've highlighted. Which I suspect relates back to step 2).

  1. What are the conditions that cause a MAC to be deleted?

    Does it depend upon the results of the checks? If so, can't it be done immediately in the checkMAC thread?

    Or is it a function of that individual MACs behaviour over time?

    Or does it depend upon the collective behaviour of two (or more) MACs? Immediately or over time?

  2. And where are you deleting it from?

    The clients hash? The network? The Auth server?

  3. Is the deletion time critical? Or order critical? Is it high or low priority?

The answer to those questions will govern what is the most appropriate approach to dealing with step 5).

If the deletion needs to be done in a ordered fashion, then it would make sense to have the check threads post a notification via a queue to the deletion thread when they've updated the hash.

But then, if all you are storing in the shared hash is true/false, then it would probably be simpler to dispense with the shared hash completely, and just post the results via the queue:  $Q->enqueue( "$mac:0" )

If it is a function of time or accumulation--(say) delete if it fails 5 times; or fails to respond for 5 minutes--then I would use a prioritised queue.

Or maybe it's just a case of we've heard nothing from that MAC for a while, so let's just free up some resources.

It all comes back to the details behind based on certain conditions based on their network activity?


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
RIP an inspiration; A true Folk's Guy

In reply to Re^10: Should I use threads? Perl/DHCP/Radius by BrowserUk
in thread Should I use threads? Perl/DHCP/Radius by Anonymous Monk

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.