in reply to Re^2: Perl Threads and multi-core CPUs
in thread Perl Threads and multi-core CPUs

OK, just to confirm - does the Perlmonks community agree that Perl threads are actually runnable on separate CPUs/cores? Can anybody tell me on how to check that separate Perl threads are running on different cores in case "top is foolong you" as suggested already? I did a test to measure the time it takes to run a single process that processes all data and then another test for 8 Perl threads that each process 1/8 of the data set and the time measurement was almost exactly the same. I am doing something wrong?

Replies are listed 'Best First'.
Re^4: Perl Threads and multi-core CPUs
by BrowserUk (Patriarch) on Sep 10, 2008 at 05:14 UTC

    I'll confirm, and guarentee, that Perl threads are scheduled by the system scheduler and are eligible to run on all available cores unless you takes specific steps to prevent them from doing so. But then I guess you'd expect me to say that :)

    I am doing something wrong?

    On the basis of your description of your observations: yes. But without some idea of what you are doing, it's pretty much impossible to suggest what that might be.

    If, for example, your process is cpu-bound and spends most of it's time interacting with your 1GB of shared data, and your threaded version is applying indiscriminate locks to that shared data, then you could effectively be serialising access to it and 7 of your threads spend their time waiting for the 8th thread to release the lock. That conceivably might produce the symptoms you are seeing.

    By way of demonstration. The following code will show almost no benefit from the threading or multiple cores:

    #! perl -slw use strict; use threads; use threads::shared; my %bighash :shared = map{ $_ => 1; } 'aaa' .. 'zzz'; sub thread { while( 1 ) { lock %bighash; 1 while each %bighash; } } my @threads = map{ threads->create( \&thread ); } 1 .. 8; $_->join for @threads;

    Because all the threads are competing for a lock on the same single data structure, so only one thread will ever doing anything useful at any given time.

    There are simple ways of avoiding this problem, but which is applicable depends upon what you are doing in your program.


    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".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: Perl Threads and multi-core CPUs
by Anonymous Monk on Sep 10, 2008 at 05:24 UTC
    Just a small nit, but the Perlmonks community doesn't have a voice :) on the other hand brother monk BrowserUk does have an extensive knowledge of how threads work :)
Re^4: Perl Threads and multi-core CPUs
by Mark.Hedges (Initiate) on Jun 27, 2014 at 17:46 UTC

    If they run on different CPU's, then why do you have to yield() to get other threads to run, on an SMP kernel?

    Update... I guess you don't. But in Coro I did have to call cede() in looping threads to let any other thread run.

    This works:

    use 5.20.0; use threads; sub loop1 { do { print "loop1\n"; sleep 1 } while 1 } sub loop2 { do { print "loop2\n"; sleep 1 } while 1 } my $thr1 = threads->create('loop1'); my $thr2 = threads->create('loop2'); do { print "main\n"; sleep 1 } while 1;

    This also works:

    use 5.20.0; use threads; my $thr1 = async { do { print "loop1\n"; sleep 1 } while 1 }; my $thr2 = async { do { print "loop2\n"; sleep 1 } while 1 }; do { print "main\n"; sleep 1 } while 1;

    This does not work:

    use 5.20.0; use Coro; async { do { print "loop1\n"; sleep 1 } while 1 } async { do { print "loop2\n"; sleep 1 } while 1 } do { print "main\n"; sleep 1 } while 1;
      If they run on different CPU's, then why do you have to yield() to get other threads to run, on an SMP kernel?

      You don't!


      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".
      In the absence of evidence, opinion is indistinguishable from prejudice.
Re^4: Perl Threads and multi-core CPUs
by Anonymous Monk on Jan 19, 2017 at 10:16 UTC

    Top is not fooling, all threads of one process share the same memory segement and hence its shown as one process.regrding time the top shows its cumulative of all the threads.u can verify this by putting a start time and end time statemetns in your script. this will be half o what you see in top

      I mean half when u have two threads