object sync monitor ref

Dmitry Olshansky dmitry.olsh at gmail.com
Thu Nov 7 03:16:38 PST 2013


07-Nov-2013 14:35, Pavel пишет:
> Hello!
>
> For every object of class there is two pointers (for vtable and monitor).
> I wonder why there is a need to have sync functionality in object (1
> pointer memory waste).
> There is also such overhead in Java and during my work with it I use
> synchronized section only for about 0.01% of all objects in program (may
> be even less). So 99.99% of objects just waste memory for that monitor
> pointer.

Agreed and I raised this point before.

>
> Why don't to introduce some annotation (property) for class needed to be
> synchronized or it descendants, according to it compiler will extend the
> size of class by the size of monitor structure (+1 allocation less).
> Synchronization will be allowed when class or supers is annotated with
> this property (it can be checked by compiler).
>

Technically there is no need to synchronize on non-shared classes at 
all. However the type-system is often violated and most code out there 
that uses synchronized on class instance undoubtedly violates it.

I'd say optimally shared(Class) should have monitor field, others don't. 
This would mean that cast to shared must not work on class references.

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list