Testing some singleton implementations

Dmitry Olshansky dmitry.olsh at gmail.com
Fri Jan 31 07:18:41 PST 2014


31-Jan-2014 17:26, Stanislav Blinov пишет:
> In fact #2, I think it's even safe to pull that store out of the
> synchronized block:
>
>          // (2)
>          if (!atomicLoad!(MemoryOrder.raw)(_instantiated))
>          {
>              // (1)
>              synchronized
>              { // <- this is 'acquire'
>                  if (_instance is null) {
//(3)
>                      _instance = new AtomicSingleton;
>                  }
>
>              } // <- this is 'release'
>
//(4)
>              // This store cannot be moved to positions (1) or (2) because
>              // of 'synchronized' above
>              atomicStore!(MemoryOrder.raw)(_instantiated, true);
>          }
>

No it's not - the second thread may get to (3)
while some other thread is at (4).

-- 
Dmitry Olshansky


More information about the Digitalmars-d mailing list