I even tried detach instead of join. Again, it progressively worsens as it reaches the 10th thread. I dont know why.

Can you let me see that code? I might then be able to answer the why.

From what I have read it looks like join blocks the other threads

Where did you read that? join() only blocks the thread is runs in.

I suspect your misunderstand arises from $_->join for @threads.

If the first thread in @threads is last to finish, then the other threads will not be joined until that occurs. But that is because you've asked to join the threads in a specific order. So don't do that.

Again, sight of your code would make life simpler.

and be able to have a hash in the main thread w the status for each MAC I have encountered.

This is the bit I'm not understanding yet.

  1. Why must the main thread retain this information?
  2. What will it do with that information?
  3. When will it do it?

By way of example. This simulates reading a logfile (it just reads from STDIN); spawns a thread for compliant input (your ack lines); those threads "process" the input (sleep and then build a hash for return); and a finisher thread processes the results (prints them to stdout).

The thing to note is the separation of concerns. The main thread only reads the log; the spawned threads only deal with their job and return results; the finisher thread gathers and processes the results. Each thread is free to do what it needs to do, when it need to do it, without any form of synchronisation or locking.

Each thread individually is a simple linear piece of code. It either starts, runs and finishes; or loops until done. No multiplexing; no synchronisation; no artificial events; no breaking up linear code into itsy bitsy chunks in order to ensure that someother concern is dealt with in a timely manner. We let the OS scheduler ensure that when a trhead needs to do something, it gets done just as soon as there is a cpu available to do it. That means that it runs just as well (actually better) on a 16 or 32 core processor as it does on a 2 core processor, without the need for any hardware specific tuning. It just works.

#! perl -slw use strict; use Time::HiRes qw[ sleep ]; use Data::Dump qw[ pp ]; use threads; use threads::shared; my $done :shared = 0; sub checkStuff { sleep rand( 5 ); return { split ' ', $_[0] }; } my $finisher = async { while( sleep 0.1 and not $done ) { for my $thr ( threads->list( threads::joinable ) ) { my $hRef = $thr->join; printf "From tid:%d got %s\n", $thr->tid, pp $hRef; } } }; while( <> ) { next unless /ACK (.+)$/; async \&checkStuff, $1; } sleep 0.1 while threads->list( threads::running ) > 1; sleep 0.1 while threads->list( threads::joinable ); $done = 1; $finisher->join; __END__ [15:14:01.92] c:\test>856740-2 ACK A 1 B 2 C 3 junk ACK A 1 B 2 C 3 ACK A 1 B 2 C 3From tid:2 got { A => 1, B => 2, C => 3 } junk ACK A 1 B 2 C 3 junk junkFrom tid:3 got { A => 1, B => 2, C => 3 } ACK A 1 B 2 C 3From tid:4 got { A => 1, B => 2, C => 3 } junk ^Z From tid:5 got { A => 1, B => 2, C => 3 }


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^6: 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.