Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re^2: Threads From Hell #1: How To Share A Hash [SOLVED]

by karlgoethebier (Abbot)
on May 15, 2015 at 18:34 UTC ( [id://1126805]=note: print w/replies, xml ) Need Help??


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»

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1126805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-25 16:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found