Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by karlgoethebier (Abbot)
on May 16, 2015 at 11:31 UTC ( [id://1126850]=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]

I'm struggling with this:

$Q->enqueue( 1000..2000, (undef) x 4 );

The docs say:

->enqueue(LIST) Adds a list of items onto the end of the queue.

I dumped it:

karls-mac-mini:monks karl$ ./queue.pl bless({ # tied threads::shared::tie queue => [ # tied threads::shared::tie 1000 .. 2000, undef, undef, undef, undef, ], }, "Thread::Queue") Took 13.820966 seconds

What are the four undefs good for?

Best regards, Karl

P.S.: But my box is faster than yours ;-)

«The Crux of the Biscuit is the Apostrophe»

Replies are listed 'Best First'.
Re^3: Threads From Hell #1: How To Share A Hash [SOLVED]
by BrowserUk (Patriarch) on May 16, 2015 at 12:46 UTC
    What are the four undefs good for?

    They cause these loops to terminate:

    while( my $number = $Q->dequeue ) {
    P.S.: But my box is faster than yours ;-)

    No surprise there. My Q6600 is over 7 years old.

    Mind you, I make better use of it than most people do of their Haswells :)


    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
      "Mind you, I make better use of it than most people do of their Haswells"

      Indeed.

      "They cause these loops to terminate"

      I guess it works like this:

      | 1000 1001 1002 1003 | | 4 threads ------------ | | 1004 1005 1006 1007 | | 4 threads ------------ | ... until they meet undef | undef undef undef undef| | 4 threads ------------ |

      Best regards, Karl (still locked/blocked)

      «The Crux of the Biscuit is the Apostrophe»

        Your diagram (whilst probably correct for this case) is too prescriptive. It implies that the work is dolled out like kids with Smarties: 1 for you, 1 for you...

        But imagine (or try) that the list of numbers was 1000 randomly chosen from 1 .. 2000, and in any order.

        Then you'd see that one thread might successively happen to get runs of small (say 1 or 2 digit) numbers that take little time; whilst another thread might happen to always get 4-digit numbers that take longer; with the result that by the end, the first thread may have processed many more items than the second.

        But they will both have processed nearly the same number of clock cycles and will end within a few milliseconds or so of each other.

        That's one of the beauties of the queue architecture is that it is inherently self-balancing:

        Outputs:

        C:\test>1126584-q [ 4] terminating at 1431869328.993618; having processed 280 values [ 1] terminating at 1431869329.076621; having processed 305 values [ 3] terminating at 1431869329.094198; having processed 190 values [ 2] terminating at 1431869329.102986; having processed 225 values Took 17.553564 seconds

        The only guarantee is that eventually all the work items are processed and each of the 4 threads will dequeue an undef and so end its loop.

        It does mean that work items cannot themselves be undef (or any non-true value unless you modify the loop to: while( defined( my $number = $Q->dequeue ) ){).

        And later versions of Thread::Queue acquired, (amongst a lot of dross), an end() method which can be used as an alternative to queueing undef's, but the implementation leaves a lot to be desired; and old (tried and tested) programming habits die hard :)


        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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 19:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found