Singleton in Action?

kdevel kdevel at
Tue Feb 5 08:20:06 UTC 2019

On Monday, 4 February 2019 at 10:17:53 UTC, bauss wrote:
> On Saturday, 2 February 2019 at 16:56:45 UTC, Ron Tarrant wrote:


>> Here's the singleton code I've been playing with:
>> 	static bool instantiated_;
>> 	// Thread global
>> 	__gshared DSingleton instance_;
>> 		if(!instantiated_)
>> 				if(!instance_)


> Ex. you state "instantiated_" is thread-local but that's the 
> flag you use to check whether it has been instantiated.

User angel has pointed to and the talk which entails the 
reasoning about that pattern.

> That will not work.

Will it not work in therory (in which?) or in practice (in which)?

> Instead it should actually be shared, especially because you 
> use it in a synchronized statement.

The purpose of the pattern is to avoid synchronisationin all but 
the first call to get().

> Also:
> DSingleton singleton = new DSingleton;
> Defeats the purpose of singleton.

Sure, but it is not lazy.

More information about the Digitalmars-d-learn mailing list