Home > Archive > PERL Miscellaneous > February 2005 > create thread from within another?
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
create thread from within another?
|
|
| gargoyle 2005-02-25, 3:58 am |
| I'm using ActivePerl 5.8.6.811 and have a question regarding threads.
What are the implications of starting a thread from within another (as
opposed to starting all threads from the main thread, ie. tid 0)? Will
any nasty bugs or strange issues come to bite me? In other words, was
the ithreads model written to handle this sort of behavior?
The reason I'm asking is because I'm trying to find a way to keep memory
footprint to a minimum, and yet be able to spawn new threads at any
given time. I figured a "dispatcher" thread could be started as early
as possible (before most modules are loaded, data structures filled,
etc.) and then when a new worker thread is needed, main could signal the
dispatcher (via a queue) and tell it to create a new thread with some
specific parameters (based on the job that needs to be accomplished at
that given moment).
Will this work, or is it just some crazy fantasy?
| |
| Anno Siegel 2005-02-25, 3:59 pm |
| gargoyle <gargoyle@no.spam> wrote in comp.lang.perl.misc:
> I'm using ActivePerl 5.8.6.811 and have a question regarding threads.
>
> What are the implications of starting a thread from within another (as
> opposed to starting all threads from the main thread, ie. tid 0)? Will
> any nasty bugs or strange issues come to bite me? In other words, was
> the ithreads model written to handle this sort of behavior?
>
> The reason I'm asking is because I'm trying to find a way to keep memory
> footprint to a minimum, and yet be able to spawn new threads at any
> given time. I figured a "dispatcher" thread could be started as early
> as possible (before most modules are loaded, data structures filled,
> etc.) and then when a new worker thread is needed, main could signal the
> dispatcher (via a queue) and tell it to create a new thread with some
> specific parameters (based on the job that needs to be accomplished at
> that given moment).
What makes you think you can conserve memory that way?
Unlike a child process, a thread handles concurrency in the address
space of one process. It doesn't duplicate everything you have loaded
at the point of dispatch.
Anno
| |
| gargoyle 2005-02-25, 3:59 pm |
| On 2005-02-25, Anno Siegel <anno4000@lublin.zrz.tu-berlin.de> wrote:
> What makes you think you can conserve memory that way?
This short paragraph (in perlthrtut):
Performance considerations
The main thing to bear in mind when comparing ithreads to
other threading models is the fact that for each new
thread created, a complete copy of all the variables and
data of the parent thread has to be taken. Thus thread
creation can be quite expensive, both in terms of memory
usage and time spent in creation. The ideal way to reduce
these costs is to have a relatively short number of long-
lived threads, all created fairly early on - before the
base thread has accumulated too much data.
> Unlike a child process, a thread handles concurrency in the address
> space of one process. It doesn't duplicate everything you have loaded
> at the point of dispatch.
I don't understand this. The man page explains that all existing variables
get copied over to the new thread (except they don't get shared unless
you explicitely request that). I tried starting threads earlier in the
program and noticed /substantial/ memory savings (several MB). And that
was just my own data structures taking up all that space! I haven't even
gotten to the point of replacing all the "use SomeModule;" lines with an
equivalent "require SomeModule; import SomeModule;" to avoid incurring
the overhead of unnecessary modules in threads which don't need them...
But back to the original question. Can I spawn a thread from within
another, or will that break anything?
| |
| kongyew@w-manager.com 2005-02-26, 8:57 pm |
| Hi,
I thought cost of thread creation is smaller than process.
kongyew@w-manager.com
|
|
|
|
|