in reply to Re^5: Should I use threads? Perl/DHCP/Radius
in thread Should I use threads? Perl/DHCP/Radius
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.
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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Should I use threads? Perl/DHCP/Radius
by MonkeyMonk (Sexton) on Aug 25, 2010 at 15:48 UTC | |
by BrowserUk (Patriarch) on Aug 25, 2010 at 16:21 UTC | |
by MonkeyMonk (Sexton) on Aug 26, 2010 at 08:53 UTC | |
by BrowserUk (Patriarch) on Aug 26, 2010 at 10:07 UTC | |
by zentara (Cardinal) on Aug 26, 2010 at 11:20 UTC |