Threads and static initialization.

Pelle Månsson pelle.mansson at gmail.com
Fri Dec 17 23:56:37 PST 2010


On 12/18/2010 07:53 AM, Jonathan M Davis wrote:
> On Friday 17 December 2010 19:52:19 Vladimir Panteleev wrote:
>> On Sat, 18 Dec 2010 03:06:26 +0200, Jonathan M Davis<jmdavisProg at gmx.com>
>>
>> wrote:
>>> And how about every other variable?
>>
>> I'm sorry, I'm not following you. What other variables?
>>
>> * Globals and class/struct/function statics are in TLS
>> * Explicitly shared vars are in the data segment
>> * Locals are in the stack or registers (no problem here)
>> * Everything else (as referenced by the above three) is in the heap
>
> Value types which on the stack are going to be okay. That's true. You're right.
> They wouldn't be in TLS.
>
> However, anything that involves the heap wouldn't be okay, and that's a _lot_ of
> variables. Any and all references and pointers - inluding dynamic arrays - would
> be in TLS unless you marked them as shared. So, you'd have to use shared all
> over the place except in very simple cases or cases where you went out of your
> way to avoid using the heap.
>
> D is designed to avoid using shared memory except in cases where data is
> immutable. So, if you try to set up your program so that it uses shared memory
> primarily, then you're going to have problems. And not calling the static
> constructors on thread creation would mean using shared memory for everything
> which uses the heap. You couldn't even create local variables which are class
> objects using TLS in such a case, because they might have a static constructor
> which then would never have been called.
>
> Really, I don't think that trying to avoid calling static constructors is going
> to work very well. It may very well be a good reason to minimize what's done in
> static constructors, but skipping them entirely would be very difficult to pull off
> safely.
>
> - Jonathan M Davis

The heap is the heap is the heap. You can have local variables on the 
heap which are not shared. I think you are overstating the need for 
shared, probably some misunderstanding.

You could not have classes/structs with static members, or call 
functions with static variables. Everything else should work, probably.

The spawned thread could use the parent thread immutable globals, to 
avoid the need to construct them in the spawned tls. I don't know if 
this is actually possible :-)


More information about the Digitalmars-d mailing list