synchonized? correct use? Thread-safe Singleton

BCS ao at pathlink.com
Fri Jan 4 10:48:59 PST 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;
 }




More information about the Digitalmars-d-learn mailing list