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

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).)

So I can use this optimisation if I need run a single specific thread, but if I need to start a bunch of identical worker threads it won't work.

  • Comment on Re^15: Strange memory leak using just threads (forks.pm)

Replies are listed 'Best First'.
Re^16: Strange memory leak using just threads (forks.pm)
by BrowserUk (Patriarch) on Sep 23, 2010 at 09:48 UTC

    No. It is N times as effective for N identical threads as it is for 1.

      No, it's not:

      use strict; use warnings; use 5.010; use threads; use Chart::Clicker; use Time::HiRes qw(time); my $N = 32; my $t = time; my @thr; push @thr, threads->create( sub { 1; } ) for 1 .. $N; $_->join for (@thr); say time - $t;

      Runs in 5.57 sec.

      use strict; use warnings; use 5.010; use threads; use Time::HiRes qw(time); my $N = 32; my $t = time; my @thr; push @thr, threads->create( sub { require Chart::Clicker; } ) for 1 .. + $N; $_->join for (@thr); say time - $t;

      Requires 34 sec to finish

        Did you actually read the examples I gave in my post before last?

        To summarise:

        • If you have an app that has worker threads that don't need Tk, and a main thread that does.

          use any moduels required by the workers; spawn the workers; then require Tk in the main thread.

        • Alternatively, it can make sense to spawn a (single) thread that then requires the 'big module' that only it needs; and then require the modules needed by the worker threads before spawning them.

          See the DBI example.

        All of this seems pretty clear from my post before last. To arrive at your interpretation on my intent seems hard.


        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.