Threads and Static Data

"Jérôme M. Berger" jeberger at free.fr
Tue Dec 11 10:48:51 PST 2007


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

		Jerome
- --
+------------------------- Jerome M. BERGER ---------------------+
|    mailto:jeberger at free.fr      | ICQ:    238062172            |
|    http://jeberger.free.fr/     | Jabber: jeberger at jabber.fr   |
+---------------------------------+------------------------------+
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHXtuTd0kWM4JG3k8RAscyAKCqAJP8MVnF6Qzbukril7bMgH8sYgCePrCC
gbNfsqvWLQdNpOaTVm1liOs=
=DurB
-----END PGP SIGNATURE-----



More information about the Digitalmars-d mailing list