Low-Lock Singletons In D
Steven Schveighoffer
schveiguy at yahoo.com
Tue May 7 07:45:38 PDT 2013
On Tue, 07 May 2013 10:33:13 -0400, David Nadlinger <see at klickverbot.at>
wrote:
> On Tuesday, 7 May 2013 at 06:50:16 UTC, Mehrdad wrote:
>> As far as I can see, there shouldn't be a need for any other handshake
>> in this example.
>>
>> As long as the object is fully initialized before _static is written to
>> (easy enough with just a memory barrier), there is no penalty for
>> subsequent reads whatsoever.
>>
>> Right?
>
> The issue is that the write to _static might never appear on the other
> threads, thus leading to multiple instances being created - even though
> it is atomic in the sense that you never end up reading a pointer with
> e.g. only half of the bytes updated.
I don't think that is an issue. The write to _static is protected by a
lock, which should present a consistent view of it inside the lock.
Mehrdad corrected his version right away that you need to check the value
inside the lock again. This essentially is classic double-checked
locking, with one check being a virtual table lookup.
-Steve
More information about the Digitalmars-d
mailing list