ratoa has asked for the wisdom of the Perl Monks concerning the following question:

hello every monks here, I have a problem about threads memory usage...
it looks like when I create a new thread ( using threads->create ), the child thread will copy every data from main thread, which can cause a huge amount of memory usage.
but if I put those data declaration after child thread had been created , then the memory usage is normal...

my question is: is there a way to prevent child thread from copying main thread's data structure? or the best practice is using Coro instead of threads ? thanks :p

Replies are listed 'Best First'.
Re: threads memory usage...
by Corion (Patriarch) on Jun 04, 2011 at 21:17 UTC

    threads and Coro are distinctly different. Coro only implements cooperative multithreading and will never use more than one CPU.

    threads by design copies all data from the main thread, so the best approach is to spawn your worker threads as soon as possible and only then allocate data per-thread.

    There is lots of discussion of threads and Coro here - Super Search will likely find these for you.

      to Corion: thank you :p
      now I get it, I've been stuck between writing non-blocking method for <Coro> and reducing <threads> memory usage.
      still, it would great if someone can combine the advantage from the both :(