views:

642

answers:

3

My guess is that class variables ("class var") are truly global in storage (that is, one instance for the entire application).

But I am wondering whether this is the case, or whether they are thread in storage (eg similar to a "threadvar") - once instance per thread.

Anyone know?

Edit: changed "scope" to "storage" as this is in fact the correct terminology, and what I am after (thanks Barry)

+7  A: 

Yes, class variables are globally scoped. Have a look in the RTL source for details of how threadvars are implemented. Under Win32 each thread can have a block of memory allocated automatically to it on thread creation. This extra data area is what is used to contain your threadvars.

Daniel
Graza
Daniel's answer is correct and I voted it up. I do wonder if you can use a little trickery by referring the class var to a thread var? Haven't tried - and not gonna try either :)
Lars Fosdal
@Lars - I'd suspect that referring a *standard* class field, rather than a "class var" to the threadvar would suffice. If the life of an object/variable is outside of an instance of the referring object, and a threadvar is needed, this lifetime is already provided, so no need for further trickery.
Graza
^ BTW, I'm also wondering the same thing, but *only* for interests sake. And in fact this question was also for interest's sake (I don't need this info immediately), and was sparked by another question someone asked regarding class variables.
Graza
I've accepted this answer, it seems we all agree/know it to be correct. Would be good to get an external reference here however, for the sake of anyone in the future who may stumble upon it..
Graza
+5  A: 

Class variables are scoped according to their member visibility attributes, and have global storage, not thread storage.

Scope is a syntactic concept, and relates to what identifiers are visible from where. It is the storage of the variable that is of concern here.

Barry Kelly
Good point - I had a feeling my semantics weren't quite right...
Graza
+1  A: 

Class variables are just like classes: global and unique for the application.