I want to add a Phobos module with template mixins for common idioms.
Diggory
diggsey at googlemail.com
Sun May 19 01:36:23 PDT 2013
On Saturday, 18 May 2013 at 16:58:19 UTC, Idan Arye wrote:
> OK, I implemented everything and made a pull request:
> https://github.com/D-Programming-Language/phobos/pull/1294
Nice, but the singleton implementation seems somewhat
over-complicated, and the low-lock singleton is broken, possibly
as a result of the former problem.
You need two variables of type T (assuming T is the target
class). One should be __gshared, the other thread-local. When
"instance()" is called, first check if the thread-local variable
is non-null, if so just return it. Otherwise enter a synchronized
block, check if the __gshared variables is null (and if so
initialise it) then copy its value to the thread-local variable
and finally return it.
For a "hasInstance" method to make any sense, the caller must be
able to synchronize on the same object that "instance()" uses to
synchronize on when it accesses the __gshared variable. Otherwise
the return value is out of date before it's even been returned.
More information about the Digitalmars-d
mailing list