Semantics of shared
Walter Bright
newshound1 at digitalmars.com
Wed May 13 22:16:14 PDT 2009
Matt wrote:
> There was just a post to reddit announcing that thread local storage
> would be the default for global variables and that the 'shared'
> qualifier would make this happen. What I can't find is a
> description of typing rules surrounding 'shared'. From the
> discussion at reddit, it sounded like 'shared' was intended to mean
> 'possibly shared', with the implication that thread local objects can
> be treated as 'possibly shared'.
>
> The problem I see with this is that it implies that it is not safe to
> assign one shared reference to another, because the former may
> actually be thread local while the latter is actually global. This
> would seem to make the "maybe shared" concept pretty useless. Is
> this not a problem? Or if not, can someone clarify to me what the
> actual semantics & typing rules are?
The addresses of thread local data cannot be implicitly cast to shared.
If they could, then they could be accessed by other threads, and the
whole safety of them being thread local is compromised.
Shared data must be created as shared, or cast to be shared. Casting to
shared is an implicitly unsafe operation, relying on the user ensuring
that it is safe to do so.
Thread local data can point to shared data, but cannot be implicitly
cast to shared.
More information about the Digitalmars-d
mailing list