Sorry, don't see how this can help. Workers usually all need the same set of modules.

The point of spawning early is to avoid there being much already in memory to cause duplication. Obviously, there's no point in require instead of use if all your threads need everything. (Hence (individually).)

But, in many scenarios, whilst the threads require the same set of modules as each other, they don't need everything the main thread needs.

Hence, for example, in a Tk app that uses some background threads for long running calculations or fetching stuff from the web etc., it makes sense to use the modules need by the workers, spawn the workers; then require Tk and anything else needed by the main thread. That way, the huge Tk doesn;t get needlessly replicated into all the workers.

Similarly, if a threaded app need to use DBI, it make sense to spawn a DBI thread that requires DBI internally, and serialise DBI requests through it. It avoids duplicating DBI in all the apps other threads; and avoid complications with a DBs or DB libraries that use PIDs (rather than TIDs) for managing their internal memory.

Another example is a threaded app that processes a large volume of work items read in from a file. Spawn the work threads before reading the file, otherwise the data structure holding the file contents gets replicated into all the threads even though the don't use it directly.


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.
RIP an inspiration; A true Folk's Guy

In reply to Re^14: Strange memory leak using just threads (forks.pm) by BrowserUk
in thread Strange memory leak using just threads by MnkyBrain

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.