Threads and Static Data

Sean Kelly sean at f4.ca
Mon Dec 10 09:55:53 PST 2007


Regan Heath wrote:
> Sean Kelly wrote:
>> Craig Black wrote:
>>>
>>> Suite!  I think I've seen the term "thread local", but it never 
>>> occured to me what it meant.  Goes to show you how green I am when it 
>>> comes to threading.  I'll check it out.  Thanks.
>>
>> For what it's worth, some C/C++ compilers have "thread local" as a 
>> storage class.  I've proposed adding this to D before, but there 
>> seemed to be no interest at the time.
> 
> If I want thread local storage I just add the variables as static 
> members of the class I've derived from Thread.

Yup.  static ThreadLocal!(T) myVar;

> Does thread local storage give us some advantage over that?

If I understand you correctly, ThreadLocal(T) is simply a front-end for 
the Thread.get/setLocal routines.  It provides type safety and 
convenience but stores the data in a dynamically allocated object, and 
the one allocation per thread may or may not be something you want to 
pay for.  This is why both methods are available.


Sean



More information about the Digitalmars-d mailing list