in reply to Re^3: Sharing Hash Question
in thread Sharing Hash Question
I apologize about my original post's vagueness. I have never posted on here before and had forgotten about the tags that you can use.
Understood.
Has your program sped up your processing even slightly?
I'll assume the answer is no. There are several overlapping reasons for why that must be the answer.
The first is this:
for(my $i=0; $i<MAX_THREADS; $i++) { threads->create( \&thread, $q )->join; }
The effect of creating many threads in a loop, but also waiting inside that loop for each one to finish (join()), before starting the next, is exactly the same as if you just called the subroutine many times one after the other.
Ie. The code above is exactly the same as doing:
thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q ); thread( $q );
Except that in addition to not speeding things up, you made them take considerably longer because you added the additional overhead of starting 10 threads and of locking and manipulating shared hashes.
You can correct that by starting all the threads in the loop; and then waiting for them all to finish,
after the loop so they can run concurrently:
my @threads = map threads->create( \&thread, $q ), 1 .. MAX_THREADS; $_->join for @threads;
This will run more quickly than your code above, but still not faster than a single-threaded process doing the same work.
When you've convinced yourself that is true, come back and I'll explain why and what you can do about it.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^5: Sharing Hash Question
by jmmach80 (Initiate) on Jul 05, 2012 at 22:33 UTC | |
by BrowserUk (Patriarch) on Jul 05, 2012 at 22:34 UTC | |
by jmmach80 (Initiate) on Jul 07, 2012 at 12:59 UTC | |
by aaron_baugher (Curate) on Jul 07, 2012 at 14:07 UTC | |
by BrowserUk (Patriarch) on Jul 10, 2012 at 02:20 UTC |