D 2.0 FAQ on `shared`

via Digitalmars-d digitalmars-d at puremagic.com
Tue Oct 21 09:36:28 PDT 2014


On Tuesday, 21 October 2014 at 16:05:58 UTC, Sean Kelly wrote:
> Also, of the shared methods that exist, there are synchronized 
> blocks but they occur at a fine grain within the shared methods 
> rather than the entire method being shared.  I think that 
> labeling entire methods as synchronized is an inherently flawed 
> concept, as it contradicts the way mutexes are supposed to be 
> used (which is to hold the lock for as short a time as 
> possible).

`shared` applies to the implicit `this` parameter, not to the 
method. It's really no different from normal parameters in this 
respect, which can either be shared, or not. There's no way to 
make them temporarily shared either (apart from casting).

I think `shared` by itself is fine, as long as it is only take to 
mean "this method can cope with the parameters being shared". 
It's `synchronized` and casting that cause the trouble, because 
they are too coarse, and synchronized doesn't specify which parts 
of an object it protects. This makes more detailed compiler 
checks impossible.


More information about the Digitalmars-d mailing list