Threads and static initialization.

Jonathan M Davis jmdavisProg at gmx.com
Fri Dec 17 22:53:04 PST 2010


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


More information about the Digitalmars-d mailing list