If you are using threads, do as little as possible that consumes memory in your main thread, that includes initialising data, before you spawn your threads. Here is timing and memory usage stats from two consecutive runs of a simple threaded script. The only difference between them is the relative position of two lines of code:

c:\test>junk Image Name PID Session Name Session# Mem Usag +e ========================= ====== ================ ======== =========== += tperl.exe 10172 0 64,840 +K Taken 3.278383 seconds c:\test>junk Image Name PID Session Name Session# Mem Usag +e ========================= ====== ================ ======== =========== += tperl.exe 2924 0 173,516 +K Taken 8.761321 seconds

For the first run, the code looked like this:

#! perl -slw use strict; use threads; use Time::HiRes qw[ time ]; sub simplesub { sleep 10, return 1 } my $start = time; my @threads = map{ threads->create( \&simplesub ) } 1 .. 10; my @array = 0 .. 1e5; my %hash = 1 .. 1e5; system qq[tasklist /fi "pid eq $$"]; printf "Taken %f seconds", time() - $start; $_->join for @threads;

For the second run, like this:

#! perl -slw use strict; use threads; use Time::HiRes qw[ time ]; sub simplesub { sleep 10, return 1 } my $start = time; my @array = 0 .. 1e5; my %hash = 1 .. 1e5; my @threads = map{ threads->create( \&simplesub ) } 1 .. 10; system qq[tasklist /fi "pid eq $$"]; printf "Taken %f seconds", time() - $start; $_->join for @threads;

So, another secret to (somewhat) lighter threads is to ensure that you spawn your threads early in the program before you generate lots of data structures in your main thread. Everything that exists in your main threads memory at the time of spawn, (including everything created by all the packages you have used ( physically before or after the point of spawn!)), will be cloned wholesale into the memory of each thread you spawn!

That has the downside that you don't always want to spawn your threads right at the start of your code as you often don't have everything they need at that point. That in turn, requires that you arrange for your threads to wait for the information they require, and some method of passing that information to them at some later point once it is available. And that introduces the complications of queues and shared memory and synchronisation.

What I've been looking for for a while now is a simple interface to a mechanism that allows me to spawn my threads early, with new, clean, uncloned, interpreters, in a suspended state and then 'resume' them, passing any parameters they require using a simple, clean interface.

my( $Xthread ) = threads->create( { suspended => 1 }, \&Xthread ); my( $Ythread ) = threads->create( { suspended => 1 }, \%Ythread ); ... Do other stuff that gets me the parameters for X $Xthread->resume( $arg1, $arg2 ); ... Generate/fetch/calculate args for Y $Ythread->resume( $Yarg1, $Yarg2 ); ... tum te tum my( @Yresults ) = $Ythread->join; ... my( @Xresults ) = $Xthread->join;

If anyone has suggestions for how to go about doing this?

If the threads could be 're-resumed' with different parameters that would be even better.


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 threads: spawn early to avoid the crush. by BrowserUk

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.