Okay. Over the course of spawning 1000 threads, this version will use `1/16th (~13MB) of the memory required by my original version above. It substitutes a shared variable for the array of thread handles, and detaches the threads so they clean themselves up rather than sitting around consuming memory (~208MB) as in the original version.

The numbers will vary depending upon how many threads exist concurrently. If you are likely to be spawning anything like this number of threads, moving to a thread pool would be your best bet, though it is slightly more complex.

Without knowing what your program is actually doing, and it dynamic resource demands, it is quite likely that this version will also not be best for your application. Hence my /msg for further information. There is no one-size-fits-all solution.

#! perl -slw use strict; use threads; use threads::shared; $|=1; our $N ||= 1_000; my $threads :shared = 0; for my $i ( 1 .. $N ) { ## if the condition is true if( $i & 1 ) { ## every odd number matches ## Start a thread to run the sub ## Passing the number as an argument ## And saving the thread object, async( \&other, $i )->detach; } } ## Wait for all the threads to finish sleep 1 while $threads; printf "Check Memory"; <STDIN>; ## Done. exit; sub other{ { lock $threads; ++$threads; } my( $number ) = @_; print "Other( $number ) starting"; ## pretend we've doing something that takes a while... for( 1 .. rand 100 ) { print "Other( $number ) busy ..."; Win32::Sleep 100; } print "Other( $number ) finished"; { lock $threads; --$threads; } }

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

In reply to Re^4: create new task from main program by BrowserUk
in thread create new task from main program by benlaw

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.