Thread-local Member Variables?

Jason House jason.james.house at gmail.com
Wed Jan 13 15:15:51 PST 2010


Nick Sabalausky Wrote:

> "dsimcha" <dsimcha at yahoo.com> wrote in message 
> news:hikj7a$18lq$1 at digitalmars.com...
> > Should there be a way, in D, to create a variable that's local to both a
> > thread and a class instance?  For example:
> >
> > class Foo {
> >    __thread uint num;
> > }
> >
> > num would basically be a reference to thread-local storage, with a new one
> > created for every instance of Foo.  Is there a (non-hacky) way to do this 
> > already?
> 
> How would that be different from doing this in D2?:
> 
> class Foo {
>    uint num;
> }
> 
> 'num' isn't declared as static, so a new one is created for every instance 
> of Foo, and 'shared' isn't used so it's thread local. Am I missing 
> something? 

Yes :(

Only globals and statics are explicitly thread local. Everything else is really just a side effect of the type system. Except for statics and globals, everything else is allocated/accessed in the same way.  If an instance is thread local, then its (unadorned) members must also be. Both shared and immutable can be passed between threads. Those modifiers are transitive, so the free sharing applies to both the instance and its members. 



More information about the Digitalmars-d mailing list