Any chance to avoid monitor field in my class?

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu May 8 10:49:01 PDT 2014


On Thu, 08 May 2014 13:21:07 -0400, Yuriy <yuriy.glukhov at gmail.com> wrote:

> On Thursday, 8 May 2014 at 14:57:37 UTC, Steven Schveighoffer wrote:
>> The de-facto minimum size of a class is 16 bytes, due to the minimum  
>> block size of the heap.
>>
>> 8 bytes vtbl pointer on 64-bit systems would still allocate into  
>> 16-byte blocks.
>>
>> -Steve
>
> Yes, but still the question remains open for non-empty classes (e.g.  
> want to use a 64bit useful payload), and for _emplacing_ any classes  
> anywhere (e.g. on stack).

To what end? What are you trying to save?

> Afaiu, there's no solution except for declaring extern(C++) (yes, i  
> know, it's a hack), and it will not work, if a class is templated on  
> something which can not be cpp-mangled. So the question is: is there any  
> reason why this is not possible? I mean, maybe this question was closed  
> long before.

It would not be derived from Object, which has the field. In other words,  
this would crash:

synchronized(cast(Object)obj) { ... }

> Also, do shared classes actually require monitors?

Perhaps you meant unshared classes? No, they don't, but a monitor is only  
allocated on demand, so you don't have to worry about it.

However, note that it is perfectly acceptable to cast to and from shared  
as long as you guarantee the uniqueness of the reference. If the monitor  
is not present, this can cause problems.

-Steve


More information about the Digitalmars-d-learn mailing list