Low-Lock Singletons In D

Steven Schveighoffer schveiguy at yahoo.com
Tue May 7 07:31:45 PDT 2013


On Tue, 07 May 2013 09:25:36 -0400, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> No. A tutorial on memory consistency models would be too long to insert  
> here. I don't know of a good online resource, does anyone?

In essence, a read requires an acquire memory barrier, a write requires a  
release memory barrier, but in this case, we only need to be concerned if  
the value we get back is not valid (i.e. NullValue).

Once in steady state, there is no need to acquire (as long as the write is  
atomic, the read value will either be NullValue or ActualValue, not  
something else).  The code in the case of NullValue must be handled very  
carefully with the correct memory barriers (Given the fact that you should  
only execute once, just insert a full memory barrier).  But that is not  
the steady state.

It's not a revolutionary design, it's basic double-checked locking  
(implemented in an unnecessarily complex way).  It can be done right, but  
it's still really difficult to get right.  The benefit of David's method  
is that it's REALLY easy to get right, and is MM independent.

-Steve


More information about the Digitalmars-d mailing list