synchronized { }

Sean Kelly sean at invisibleduck.org
Mon Jun 30 18:09:34 PDT 2008


== Quote from Jarrett Billingsley (kb3ctd2 at yahoo.com)'s article
> "Sean Kelly" <sean at invisibleduck.org> wrote in message
> news:g4bhn1$ufo$1 at digitalmars.com...
> > Um, it /is/ equivalent to synchronized(this).  What made you think
> > differently?
> Uh?  In the OP:
> -----
> Right now, if you use a synchronized statement with no argument, it will
> sync on a mutex unique to that statement.
> -----
> Doesn't seem like synchronized(this) to me.

I'd hate to disagree with Walter since he created D, but I don't know that
there's even a means of doing this in the runtime right now.  Storage is
reserved for a single global monitor which is shared by all free functions
and probably all functions in structs as well since there's no room to
store a per-instance monitor for them (I never bothered to verify the
behavior with structs, so I'm making an educated guess).  Also, each
object instance gets its own monitor which is shared by all non-static
class member functions.  Finally, static class member functions use
the monitor on the TypeInfo object for that class.

This aside, synchronizing in the manner implied by Walter's remark
would be largely useless anyway, since it's quite common for multiple
functions to share access to some resource and all such functions must
synchronize on the same mutex.  Better to be safe and synchronize
everything on a single global mutex that try for performance and end
up using multiple unrelated mutexes for the same operation.  If this
is truly the way that Phobos 2.0 works, well... I suppose that's one
more reason I'd be glad I'm using Tango.


Sean



More information about the Digitalmars-d mailing list