http://www.perlmonks.org?node_id=809056


in reply to Re: Standard way to convert timezone in multithreaded script
in thread Standard way to convert timezone in multithreaded script

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.