in reply to Re: Threads From Hell #1: How To Share A Hash [SOLVED]
in thread Threads From Hell #1: How To Share A Hash [SOLVED]
"But the biggest lesson...the obvious optimisation..."
"Du sprichst ein großes Wort gelassen aus." (Johann Wolfgang von Goethe)
Very impressive and instructional. What else should i say?
Update:
There are two little things i would like to add:
My basic theme wasn't to calculate factorials using threads.
I wanted to explore multithreading and thought the best idea would be to use some "expensive" calculation for this. Like in a classic homework.
Unfortunately you provided a much better algorithm that avoids multithreading ;-)
I fear i hit a basic problem in this context.
And apropos little quirks:
I wrote:
sub process { my $number = shift; # lock %result; $result{ threads->tid() } = shared_clone( { $number => factorial($number) } ); $semaphore->up; }
It seems like i really need to say something like:
for my $key ( sort { $a <=> $b } keys %result ) { my $ref = $result{$key}; while ( my ( $k, $v ) = each %{$ref} ) { say qq($k => $v); } }
...to iterate over the results. From the docs:
"each HASH does not work properly on shared references embedded in shared structures"
Thanks for this advice and my best regards, Karl
«The Crux of the Biscuit is the Apostrophe»
|
---|