Threads and Static Data

Craig Black cblack at ara.com
Wed Dec 12 10:25:11 PST 2007


""Jérôme M. Berger"" <jeberger at free.fr> wrote in message 
news:fjmm2j$18pc$1 at digitalmars.com...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> Regan Heath wrote:
>> Jérôme M. Berger wrote:
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA1
>>>
>>> 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.
>>>>
>>>> Does thread local storage give us some advantage over that?
>>>>
>>>     But if you want to launch several instances of this thread at the
>>> same time, your variables won't be local, will they?
>>
>> What do you mean by "local"?
>>
>> If you want a copy of the variables for each thread,
>
> That's what is usually meant by "thread local", yes.
>
>> make them non-static members of the thread.
>>
> As others have said, this only works if you have complete control
> of your environment. In particular, if you are writing a library and
> don't know if that library will be used in a single-threaded or
> multi-threaded environment, then you cannot put any data in the
> thread class since you won't have access to it in the first place.
>
> OTOH, properly designed libraries should store all their data for a
> given task in a struct (or class) and let the caller manage that
> struct however he wants (local variable, class member, global
> variable, whatever). So thread local storage isn't that useful.

I'm not following you.  Is what you are suggesting just another way to do 
thread local storage using a data structure that is instantiated each time a 
thread is instantiated?  Isn't this the exact same thing as the ThreadLocal 
template provided in Tango?  The bottom line is that data has to be 
instantiated with each thread.  I don't see a way around this. 





More information about the Digitalmars-d mailing list