Optional monitors suggestion

Martin Nowak via Digitalmars-d digitalmars-d at puremagic.com
Sat May 17 21:44:55 PDT 2014


On Wednesday, 14 May 2014 at 09:16:21 UTC, Yuriy wrote:
> On Wednesday, 14 May 2014 at 08:37:41 UTC, bearophile wrote:
>> What's the syntax to define a class without its __monitor? Are 
>> you using an annotation like @no_monitor?
> No syntax for that. The __monitor is not present by default 
> anywhere. If you need it, you need to define one.
>
> class A
> {
>
> }
>
> class B
> {
>     void* __monitor;
> }
>
> {
>     A a = new A(); // sizeof a instance is 8
>     B b = new B(); // sizeof b instance is 16
>     synchronized(a)
>     {
>         // Internally allocates a monitor and place it to 
> global monitor hash map. On synchronize, search the hash map 
> for already created monitors.

Global hashmap is a bad idea IMO because it's possibly expensive 
and impure. Rather deprecate synchronizing on classes without an 
explicit monitor.

>     }
>
>     synchronized(b)
>     {
>         // Internally allocates a monitor, and sets b.__monitor 
> to it. Pretty much like it was done before, except that 
> __monitor field may now have different offset.
>     }
A UDA on the class is cleaner than adding magic members.
> }
>
> So the semantics remains the same, you just may want to define 
> a __monitor field if you want better performance.



More information about the Digitalmars-d mailing list