valid uses of shared

Artur Skawina art.08.09 at gmail.com
Fri Jun 8 14:01:50 PDT 2012


On 06/08/12 21:30, Artur Skawina wrote:
>>> in this thread, omitting 'static' should cause a build failure; right
>>> now it is accepted, even when written as
>>>
>>>    shared Atomic!int counter;
>>>
>>> The problem? 'shared' is silently dropped. Move the counter from a struct
>>> into a function after realizing it's only accessed from one place, forget
>>> to add 'static' - and the result will compile w/o even a warning.
>>
>> The difference is that static is not a type constructor.
> 
> The problem is that 'shared' is lost, resulting in an incorrect program.
> When you explicitly declare something as shared the compiler better treat
> it as such, or fail to compile it; silently changing the meaning is never
> acceptable.

That was misleading; "shared" isn't actually lost, but as the variable is
placed on the stack it becomes effectively thread local, which can be very
unintuitive. But i can't think of an easy way to prevent this mistake, while
still allowing shared data to be placed on the stack. And the latter can
be useful sometimes...

artur


More information about the Digitalmars-d mailing list