Thread-local Member Variables?

Steven Schveighoffer schveiguy at yahoo.com
Thu Jan 14 04:49:54 PST 2010


On Wed, 13 Jan 2010 08:52:42 -0500, dsimcha <dsimcha at yahoo.com> wrote:

> 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?

Reading all the responses, it looks like what you want this to apply only  
when Foo is marked as shared?  Because without the __thread denotation  
(and without having a shared(Foo) instance), num is already thread local  
and one-per-instance.

Do you have a use case for something like this?  Maybe there's another way.

I don't see any reason to have language support for this since it's not  
simply a fight with the type system, as logical const is.  You're looking  
at least at adding a hash-table per thread.  The GC will also have to be  
involved to clean up the hash table when instances go away.

-Steve



More information about the Digitalmars-d mailing list