Low-Lock Singletons In D

Max Samukha maxsamukha at gmail.com
Fri May 24 05:49:17 PDT 2013


On Monday, 6 May 2013 at 17:58:19 UTC, Walter Bright wrote:
> On 5/6/2013 6:14 AM, Max Samukha wrote:
>> FWIW, I played with a generalized form of this pattern long 
>> ago, something like
>> (typing from memory):
>
> And, that's the classic double checked locking bug!

D man is the bug! I simply failed to insert a line that flags the
thread-local guard. Sorry for that.

The Nullable thing was an impromptu to avoid ugly specializations
I used for nullable and non-nullable types in my original
implementation. Note that the Nullable is not phobos Nullable -
the latter incurs unnecessary overhead for types that are already
nullable. Maybe, the use of Nullable is an overkill and a
__gshared boolean (redundant in case of a nullable type) would
suffice.

David mentioned the trick on this NG years ago. It is well known,
understood and would be rarely needed if D could properly do
eager initialization of global state. :P

Anyway, I popped up mainly to point out that the pattern should
not be limited to classes.


More information about the Digitalmars-d mailing list