Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
i would bet for security, a pid gets spawned into 1 core, and since threads on linux share the same master pid, all threads probably will be assigned to the same core

Can I name an amount? Because I'll take that bet :-). Sorry, but this is completely wrong, and if you think about it for a minute you'll realize that it would be completely brainless and negate the usefulness of kernel threads entirely. Yes, threads are spawned under a single PID, but they can very well use more than a single core. This is simple to demonstrate (WARNING, following code will hog your CPUs, don't run it on a production system!):

perl -Mthreads -e'$n=3;for (1..$n){$t[$_]=threads->create(sub{my $r; while(1){$r++}});}; sleep 10;threads->exit()'

Set $n to 1 less than the number of cores your machine has (in this case 3 for a quad-core). Then run the command and observe its behaviour in the system monitor of your choice (top will do). You'll see that it runs on the number of CPU cores specified, despite retaining a single PID. So your assumption is wrong.

As for the usefulness/performance benefit of updating a shared variable from threads running on several cores, I'm staying firmly out of that debate. My own gut feeling would be that it is indeed not worth it on Linux, context switching Perl threads is more expensive than doing the same with processes (or at least it was the last time I measured), and a short test with the above command line using threads::shared seems to support the feeling. But the true answer will invariably depend (almost exclusively) on the concrete problem the OP is trying to solve, so he should benchmark competing solutions to that concrete problem and find out for himself.


All dogma is stupid.

In reply to Re^2: Standard way to convert timezone in multithreaded script by tirwhan
in thread Standard way to convert timezone in multithreaded script by whale2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the leaves swirl about...

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2017-10-23 16:28 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      My fridge is mostly full of:

















      Results (281 votes). Check out past polls.

      Notices?