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

RE: Activestate 5.8.1 build 807

I started working on my first threaded perl application
and I have a problem where it would randomly exit without error

Here is some sample code that just fires up some detached
threads... it exhibits this behaviour

Whats really interesting is that $thread_count may only make it to
30 or it may make it to 700+ before the application
just exits without error... but at 700+ process monitor
only shows 3-4 threads for perl.exe

This value should be much higher based on the sleep values
in the parent and the thread handler --- right???

I'm obviously missing something...

Don't grill me too hard :-)
Thanks!!!
use strict; use warnings; use threads; my $thread_count = 0; while (1) { threads->new(\&handle_thr +ead,++$thread_count); sleep .1; } sub handle_thread { my $thread_count = shift; threads->self->detach; # so long parent print "You are thread number $thread_count \n"; sleep .2; }
--jammin

Replies are listed 'Best First'.
Re: Win32 and threads;
by BrowserUk (Patriarch) on Jul 11, 2004 at 00:24 UTC

    What did you expect to happen?

    What were you hoping to prove?

    Your sleeps are not doing what you think.

    Perl's built-in sleep takes an integer number of seconds as it's argument. So sleep .1; becomes  sleep 0;.

    That means that each of your threads:

    1. Starts
    2. Detaches itself so that it will die immediately it finishes rather than hanging around for some other thread to join it and retrieve it's return value.
    3. Print's it's message.
    4. Sleeps for no time
    5. Then finishes. Which as it is detached mean that it's resources are freed immediately.

    Hopefully, that explains why there are only ever 3 or 4 threads in the process list at any given time.

    As for why the program terminates. I could speculate that your running out of resources somewhere and segfaulting, but that doesn't explain the absence of any error message. Which OS are you running?

    Update: If your considering doing anything serious with threads, you should upgrade to 5.8.3 at least and preferably 5.8.4. Ithreads were not very stable before this.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algoritm, algorithm on the code side." - tachyon
      What did you expect to happen?

      Complete until an exhaustion of resources and then give error

      What were you hoping to prove?
      Find why my program exited without error

      Your sleeps are not doing what you think.
      Thanks.... learned something new...

      I modified the code... now I can see the thread count
      increment in process monitor but yet the perl app still
      exits at random times without error.

      I should have stated Windows 2000 Build 2195 SP4

      I'll see if activestate has an update

      Thanks for all the info

      Here is the code that still fails

      use strict; use warnings; use threads; my $thread_count = 0; for (1..5000) { threads->new(\&handle_thread,++$thread_count); } sleep 10; print "--Parent Quits --"; ###################### sub handle_thread { my $thread_count = shift; threads->self->detach; # so long parent print "You are thread number $thread_count \n"; sleep 3; }
      --jammin
        Thanks to all!!

        BrowserUk you nailed it...

        I just ran the sample code against
        5.8.3 and works fantastic.... 5.8.1 is not thread stable

        Thanks again

        --jammin
Re: Win32 and threads;
by NetWallah (Canon) on Jul 11, 2004 at 00:08 UTC
    I have no problem with running your program on my machine, except it is scary to start so many threads. I killed it after 2500.

    Here is a modified version of your program, which runs flawlessly (at 100% CPU) , and produces 5000 threads.

    I'm on a Windows XP machine @ 1 GHZ, 1 G memory.
    perl, v5.8.0 built for MSWin32-x86-multi-thread.

    use strict; use warnings; use threads; my $thread_count = 0; for (1..5000) { threads->new(\&handle_thread,++$thread_count); sleep .1; } sleep 10; print "--Parent Quits --"; ###################### sub handle_thread { my $thread_count = shift; threads->self->detach; # so long parent print "You are thread number $thread_count \n"; sleep .2; }

        Earth first! (We'll rob the other planets later)