in reply to Re: Avoiding data copying in threads in thread Avoiding data copying in threads
Hi x-lours,
Thanks for this suggestion.
However, isn't the use semaphore is more towards synchronization of threads (or processes)? I am using Thread::Queue to sync and communicate between threads.
And, so synchronization and communication is not an issue.
My main worry is about this behavior referred on this page:
"It means that every time you start a thread all data structures are copied to the new thread. And when I say all, I mean all. This e.g. includes package stashes, global variables, lexicals in scope. Everything!"
Thanks
Re^3: Avoiding data copying in threads
by Preceptor (Deacon) on Jul 01, 2014 at 09:37 UTC
|
When you 'create' a thread - at the point at which you 'create' - the process state is copied.
That can get quite expensive on memory overhead, yes. That is the way of things if you want to use Perl threads. You can mitigate it a little, by limiting memory footprint of your originator, and 'create' before you start filling memory with stuff you don't need in every thread.
If you're on a system that supports it natively (e.g. most Unixes) then fork() may be an alternative - as most kernels implement copy-on-write for forked processes, meaning that they're very memory efficient. However you lose some of the conveniences that you'd get from e.g. threading.
| [reply] |
|