in reply to Win32 and threads;

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

Replies are listed 'Best First'.
Re^2: Win32 and threads;
by jammin2night (Initiate) on Jul 11, 2004 at 01:10 UTC
    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
        Note: this code works fine under
        v5.8.4 built for i386-freebsd-thread-multi-64int

        I'm testing with
        v5.8.4 built for MSWin32-x86-multi-thread - build 810 (activestate)

        When I execute the following code if $thread_sleep = "50" my application
        dies at threads_count == 119 religiously..

        It exits with NO ERROR !!

        if $thread_sleep = "3" then $thread_count reaches it's goal and exits...
        I assume this is because older threads are exiting sooner

        Is there a limit of threads per process under Win32 ??

        Why don't I get any error ??

        I'm using Windows 2000 - Build 2195 SP4 + patches

        Thanks!
        use strict; use warnings; use threads; my $thread_sleep = "50"; 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 $thread_sleep; }
        --jammin