synchonized? correct use? Thread-safe Singleton

BLS nanali at nospam-wanadoo.fr
Fri Jan 4 10:59:36 PST 2008


BCS schrieb:
> Reply to bls,
>>>> BCS schrieb:
>>>>>
>>>>> template Singleton(T)
>>>>> {
>>>>> public:
>>>>> static T instance() sychronized
>>>>> // according to the language spec this should work
>>>>> {
>>>>> if(_instance is null) _instance = new T;
>>>>> return _instance;
>>>>> }
>>>>> private:
>>>>> this() {}
>>>>> static T _instance;
>>>>> }
>>>>> class Application : Singleton!(Application)
>>>>> {
>>>>> mixin(Singleton!(typeof(this)));
>>>>> }
> 
>>
>> class Application // no ": ..."
>>
>> Ah, that makes more sense :)
>>
>> Okay .
>> Now; to make it smart .
>> class Application
>> {
>> class Singleton
>> {
>> }
>> }
>> You mean ... shi. ?
>>
> 
> Almost. It might not be important, but because Singleton is a template 
> rather than a templated class, the inner class you show doesn't exist.
> 
> You more or less end up getting this:
> 
> class Application
> {
>  public:
>  static Application instance() sychronized
>  // according to the language spec this should work
>  {
>   if(_instance is null) _instance = new Application;
>   return _instance;
>  }
>  private:
>  this() {}
>  static Application _instance;
> }
> 
> 
Okay, I see.
Last question,(promised) As far as I can remember Sean and I talked 
about to use volatile ...
here it is
http://www.digitalmars.com/d/archives/digitalmars/D/Threadsafe_singleton_using_volatile_synchonized_53768.html

What do you think ?
Bjoern


More information about the Digitalmars-d-learn mailing list