Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

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

by karlgoethebier (Abbot)
on May 14, 2015 at 17:13 UTC ( [id://1126680]=note: print w/replies, xml ) Need Help??


in reply to Re: Threads From Hell #1: How To Share A Hash
in thread Threads From Hell #1: How To Share A Hash [SOLVED]

Thanks again BrowserUK.

But must i really say lock %result?

Without it the calculation for 2000 numbers is about two times faster twice as fast.

Edit: Minor change of wording.

Best regards, Karl

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^3: Threads From Hell #1: How To Share A Hash
by BrowserUk (Patriarch) on May 14, 2015 at 18:59 UTC
    But must i really say lock %result?

    If you can guarantee no duplicates in your list of numbers; then no.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    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". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
      "...guarantee no duplicates..."

      Sure can i - in this case ;-)

      But the code works with dups, whether i use lock or not.

      I tried this:

      sub process { my $number = shift; # lock %result; $result{ threads->tid() } = shared_clone( { $number => factorial($number) } ); $semaphore->up; }

      It works as expected:

      ./semaphore.pl 0.0276410579681396 { # tied threads::shared::tie 1 => { # tied threads::shared::tie 10 => 3628800, }, 2 => { # tied threads::shared::tie 10 => 3628800, }, 3 => { # tied threads::shared::tie 10 => 3628800, }, 4 => { # tied threads::shared::tie 11 => 39916800, }, }

      Best regards, Karl

      «The Crux of the Biscuit is the Apostrophe»

        But the code works with dups, whether i use lock or not.

        It will ... until it doesn't. And then it won't ... silently.

        Eg. In this code:

        #! perl -slw use strict; use threads; use threads::shared; use List::Util qw[ shuffle ]; use Time::HiRes qw[ time ]; our $N //= 1e3; our $W //= 20; our $L //= 1; my %counts :shared; sub worker { my @order = shuffle 1 .. 20; for( 1 .. $N ) { $L and lock %counts; ++$counts{ $_ } for @order; } } my $start = time; $_->join for map threads->new( \&worker ), 1 .. $W; my $end = time; for( sort{ $a <=> $b } keys %counts ) { printf "%2u : %u\n", $_, $counts{ $_ }; } printf "Took %.6f seconds\n", $end - $start;

        each of the counts should end up as $N * $W, which they do with locking enabled:

        C:\test>junk2 -N=1000 -W=32 -L=1 1 : 32000 2 : 32000 3 : 32000 4 : 32000 5 : 32000 6 : 32000 7 : 32000 8 : 32000 9 : 32000 10 : 32000 11 : 32000 12 : 32000 13 : 32000 14 : 32000 15 : 32000 16 : 32000 17 : 32000 18 : 32000 19 : 32000 20 : 32000 Took 2.674634 seconds

        But turn off the locking, and whilst the iThreads architecture will prevent internal corruption, thus no crashes, the results are silently corrupted:

        C:\test>junk2 -N=1000 -W=32 -L=0 1 : 25459 2 : 25272 3 : 25198 4 : 25176 5 : 25543 6 : 25338 7 : 25168 8 : 25261 9 : 25080 10 : 24649 11 : 24985 12 : 25527 13 : 25499 14 : 25351 15 : 25293 16 : 25337 17 : 25380 18 : 25224 19 : 25403 20 : 25123 Took 16.446213 seconds

        And, it took 8 times longer to produce the incorrect output.

        Locking is the programmer's responsibility, and the programmer's choice; but unless you're sure that you can get away without it, it's best to err on the side of caution.

        Whilst your code, as is, doesn't require it; it's best not to get into the habit of omitting it, until you are very confident that you know when you can do so safely.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        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". I'm with torvalds on this
        In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked
        p

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2024-04-19 22:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found