Thread overview
synchonized? correct use? Thread-safe Singleton
Jan 04, 2008
BLS
Jan 04, 2008
BCS
Jan 04, 2008
BLS
Jan 04, 2008
BCS
Jan 04, 2008
BLS
Jan 04, 2008
BCS
Jan 04, 2008
BLS
Jan 04, 2008
BCS
Jan 04, 2008
BLS
Jan 04, 2008
BLS
January 04, 2008
Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K allready explains it to me, but I 've lost the info. :(
Bjoern

class 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)
{
}
January 04, 2008
Reply to bls,

> Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K
> allready explains it to me, but I 've lost the info. :(
> Bjoern
> class 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)
> {
> }

I think this will fail because Singleton!(Application).instance() will call Application's this() and that is private.

IMHO, a better approach (that I haven't tested) would be to use a mixin:

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)));
}


January 04, 2008
BCS schrieb:
> Reply to bls,
> 
>> Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K
>> allready explains it to me, but I 've lost the info. :(
>> Bjoern
>> class 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)
>> {
>> }
> 
> I think this will fail because Singleton!(Application).instance() will call Application's this() and that is private.
> 
> IMHO, a better approach (that I haven't tested) would be to use a mixin:
> 
> 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)));
> }
> 
> 

Thanks BCS,

Confused  ...'cause virus infected (me) not the pc


class Application : Singleton!(Application)
{
  mixin(Singleton!(typeof(this)));
  this()
  {
    return instance(this)
  }
}

Honk, that stinks somehow !

Hope I ask not for too much, but please explain the use of the mixin
Bjoern
January 04, 2008
Reply to bls,

> BCS schrieb:
> 
>> Reply to bls,
>> 
>>> Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K
>>> allready explains it to me, but I 've lost the info. :(
>>> Bjoern
>>> class 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)
>>> {
>>> }
>> I think this will fail because Singleton!(Application).instance()
>> will call Application's this() and that is private.
>> 
>> IMHO, a better approach (that I haven't tested) would be to use a
>> mixin:
>> 
>> 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)));
>> }
> Thanks BCS,
> 
> Confused  ...'cause virus infected (me) not the pc
> 
> class Application : Singleton!(Application)
> {
> mixin(Singleton!(typeof(this)));
> this()
> {
> return instance(this)
> }
> }
> Honk, that stinks somehow !
> 

Oops, typo!!

class Application // no ": ..."

> Hope I ask not for too much, but please explain the use of the mixin

Think of a mixin as a copy/paste + macro. Instance the template (that's the macro part) and just lump it into whatever scope you use it in (the copy/paste part). It's a quick way to do boiler plate coding.

> Bjoern
> 


January 04, 2008
BCS schrieb:
> Reply to bls,
> 
>> BCS schrieb:
>>
>>> Reply to bls,
>>>
>>>> Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K
>>>> allready explains it to me, but I 've lost the info. :(
>>>> Bjoern
>>>> class 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)
>>>> {
>>>> }
>>> I think this will fail because Singleton!(Application).instance()
>>> will call Application's this() and that is private.
>>>
>>> IMHO, a better approach (that I haven't tested) would be to use a
>>> mixin:
>>>
>>> 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)));
>>> }
>> Thanks BCS,
>>
>> Confused  ...'cause virus infected (me) not the pc
>>
>> class Application : Singleton!(Application)
>> {
>> mixin(Singleton!(typeof(this)));
>> this()
>> {
>> return instance(this)
>> }
>> }
>> Honk, that stinks somehow !
>>
> 
> Oops, typo!!
> 
> class Application // no ": ..."
> 
>> Hope I ask not for too much, but please explain the use of the mixin
> 
> Think of a mixin as a copy/paste + macro. Instance the template (that's the macro part) and just lump it into whatever scope you use it in (the copy/paste part). It's a quick way to do boiler plate coding.
> 
>> Bjoern
>>
> 
> 
Thanks BCS:
>>>>> class Application // no ": ..."

Ah, that makes more sense :)

Okay .
Now; to make it smart .

class Application
{
  class Singleton
  {
  }
}
You mean ... shi. ?

very very big grin
Seriously? I need a bullet proof thread safe singleton implementation ..

Hell I should change my BLS into LS , people here are sometimes confused.
Bjoern
January 04, 2008
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;
}


January 04, 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
January 04, 2008
Reply to bls,

> 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_singlet
> on_using_volatile_synchonized_53768.html
> 
> What do you think ?
> Bjoern

Thread safety isn't my thing, sorry.


January 04, 2008
BCS schrieb:
> Reply to bls,
> 
>> 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_singlet
>> on_using_volatile_synchonized_53768.html
>>
>> What do you think ?
>> Bjoern
> 
> Thread safety isn't my thing, sorry.
> 
> 
Anyway, thanks for spending your time :)
January 04, 2008
BLS schrieb:
> Do I have a THread-safe singleton ? If not HowTo..   Sorry. Sean K allready explains it to me, but I 've lost the info. :(
> Bjoern
> 
> class 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)
> {
> }

Okay, I'll have to think more about this stuff.
In case that somebody can offer a templated (thread-safe and bulletproof) Singleton class. ... THANKS
Bjoern