What is shared functions?

Jonathan M Davis jmdavisProg at gmx.com
Sun Oct 23 11:31:44 PDT 2011


On Sunday, October 23, 2011 20:22:01 simendsjo wrote:
> On 23.10.2011 20:14, Jonathan M Davis wrote:
> > On Sunday, October 23, 2011 14:32:34 simendsjo wrote:
> >> What does shared for functions mean? I thought it was supposed to
> >> automatically synchronize access, but this doesn't seem to be the
> >> case.
> >> 
> >> void f() shared {
> >> // no synchronization
> >> }
> >> 
> >> void f() {
> >> 
> >>     synchronized {
> >>     
> >>       // do stuff
> >>     
> >>     }
> >> 
> >> }
> > 
> > shared doesn't automatically synchronize anything. I believe that it
> > makes some guarantees about instruction ordering not being messed with
> > by the compiler, but I'm not sure. I'd have to go look it up in TDPL
> > though. Regardless, on a _function_, I don't think that shared does
> > anything. D probably ignores it. It tends to do that with incorrect
> > attributes. It _might_ do something though. I don't know.
> > 
> > - Jonathan M Davis
> 
> Guess it's about time to buy TDPL.
> I remember D ignoring protection attributes before, but this is a bug now:
> private public class C {}
> t.d(1): redundant protection attribute
> 
> So I guess shared has an effect on functions (or the missing error is a
> bug).

Not necessarily. There are a number of attributes that I belive that it will 
continue to ignore in the interest of reducing errors with generated code. 
Just because it happens to complain in one particular case doesn't mean that 
it's been fixed in general. I believe that it's always been the case that dmd 
complains in some cases and not in others. It _is_ a bit annoying though. 
Maybe it would cause problems for generated code, by my general take on it is 
that dmd should _never_ ignore attributes, but that's not the way that it 
works unfortunately,

So, it may be that shared does something here. It may be that it's ignored, 
and if it's ignored it may or may not be a bug that it compiles without error.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list