konnjuta has asked for the wisdom of the Perl Monks concerning the following question:

Can someone explains why this code only runs under one CONCURRENT thread.

perl -Mthreads -Mbignum -le '$_->join foreach map async { 2**859433-1 } , 1..2'

Process/threads listing

root@blackjack:/usr/bin# ps -efL | grep perl root 10694 8844 1 1 0 10:32:34 pts/1 0:00 grep pe +rl usr 10679 9335 1 1 3 10:32:28 pts/2 0:06 perl -M +threads -Mbignum -cle $_->join foreach map async { 2**859433-1 } , 1. +.2

But by only changing the subroutine this runs across 3 CONCURRENT threads.

perl -Mthreads -Mbignum -le '$_->join foreach map async { while ($i++ < 2) {$j++; $j *= 1.1 for (1..9999)} }, 1..2'

Process/threads listing

root@blackjack:/usr/bin# ps -efL | grep perl root 10757 8844 1 1 0 10:32:46 pts/1 0:00 grep pe +rl usr 10755 9335 1 3 0 10:32:44 pts/2 0:00 perl -M +threads -Mbignum -le $_->join foreach map async { while ($i++ < 2) {$ +j++ usr 10755 9335 2 3 1 10:32:44 pts/2 0:02 perl -M +threads -Mbignum -le $_->join foreach map async { while ($i++ < 2) {$ +j++ usr 10755 9335 3 3 1 10:32:44 pts/2 0:02 perl -M +threads -Mbignum -le $_->join foreach map async { while ($i++ < 2) {$ +j++

Replies are listed 'Best First'.
Re: Perl threading problem?
by Athanasius (Archbishop) on Jan 17, 2014 at 10:00 UTC

    Hello konnjuta,

    You don’t say how you are determining the number of threads. But here is my guess as to what you are seeing:

    The second script compiles quickly and creates 2 new threads, which together with the parent thread make 3 in all.

    The first script sees 2**859433-1 and tries to calculate that during the compile phase, and so — since the calculation takes a long time — no threads are actually spawned, because the script never actually runs. The “one thread” you observe is really the perl interpreter compiling the script prior to running it.

    Run both scripts with perl -c ... to verify.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      Thank you Athanasius. I forgot to mention the keyword CONCURRENT threads. Your explanation regarding compile phase make alot of sense.
Re: Perl threading problem?
by BrowserUk (Patriarch) on Jan 17, 2014 at 13:47 UTC

    Two threads do run:

    perl -Mthreads -E"$_->join foreach map async { say threads->tid,' ', 2 +**859433-1 } , 1..2" 1 1.#INF 2 1.#INF

    So the problem is with your observation; not Perl or threading.


    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.

      Your comment might be correct, but it could also be interpreted in the sense of the OP in that changing the subroutine changes the number of threads being used.

      From my experiments I do follow Athanasius' explanation.

        But even so, both threads do run eventually, it just takes a while before they get spawned:

        C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**8-1 } , 1..2" 1 2 C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**85-1 } , 1..2" 1 2 C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**859-1 } , 1..2" 1 2 C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**8594-1 } , 1..2" 1 2 C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**85943-1 } , 1..2" 1 2 C:\test>perl -Mthreads -Mbignum -wE"$_->join foreach map async { say t +hreads->tid; 2**859433-1 } , 1..2" 1 2

        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.

        Oh. I see. It is the use bignum that prevents it from reaching the point where the threads spawn.

        How very strange ... though it's not the first time I've seen problems caused by these overload everything in sight modules.


        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.