#Previous question #Index of C++ Questions #Next question

Singleton class does not support object creation/deleting using new and operator. Thus the constructor and destructor have to be private so that it can prevent user from doing so.

In most of the design singleton class provides a interface function like GetInstance() to return an instance to the outside world and also a Release() function to inform the class server that client has finished using this object.

Also a global object retention count has to be there to monitor the reference count of the object usage. At the very beginning, reference count will be zero and global object pointer is NULL. When a first client calls GetInstance() it allocates the object and increments the reference count by one. If another GetInstance() call comes it does not allocate another object, instead it returns the existing global instance and increments the reference count.

Now client calls Release() whenever client finishes using the object . Suppose we have reference count equal to 2. New client calls Release(). At this time it decrements the reference count by one. Thus count will be 1. Now the last client calls Release() and object reference count becomes zero. Now there is no client active. Thus holding the object in memory is a wastage. We can now safely release the memory.

There is also one design aspect when dealing in multi-threading/multi-programming environment. We must handle the increment and decrement of the reference count in a thread-safe manner. We must use a critical section inside GetInstance() and Release() function when referring the global reference count.

Example:

class singleton
{
private:
  static singleton *self;
  static unsigned int ref_count;
  singleton()
  {
    ref_count = 0;
  }
  ~singleton()
  {
    ref_count = 0;
  }
public:
  static singleton * GetInstance(void);
  void Release(void)
  {
    /*Start of critical section*/
    if(self)
    {
      ref_count--;
      if(ref_count == 0)
      {
        delete self;
      }
      
    }
    /*End of critical section*/
  }
};
singleton * singleton::GetInstance(void)
{
  /*Start of critical section*/
  if(!self)
  {
    self = new singleton();
  }
  ref_count++;
  return self;
  /*End of critical section*/
}
singleton * singleton::self = NULL;
unsigned int singleton::ref_count = 0;
void main(int argc, char *argv[])
{
  singleton *s_ptr1, *s_ptr2;
  /*ref_count = 0 -> 1, self = NULL -> allocated*/
  s_ptr1 = singleton::GetInstance();
  /*ref_count = 1 -> 2, self = no allocation*/
  s_ptr2 = singleton::GetInstance();
  /*ref_count = 2 -> 1, self = no de-allocation*/
  s_ptr1->Release();
  /*ref_count = 1 -> 0, self = de-allocated, -> NULL*/
  s_ptr2->Release();
}

You have viewed 1 page out of 62. Your C++ learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

object-oriented, C vs C++, C++ vs Java, encapsulation, constructor, overloaded constructor, destructor, destructor overloaded, copy constructor, copy constructor, deep copy, deep copy vs shallow copy, singleton, default access modifier, default access modifier, default access modifier, abstraction, THIS pointer, static function, THIS pointer, a static function, scope resolution operator, inline function, private vs protected, const function, polymorphism, operator overloading, function overloading, overriding, prefix vs postfix, friend class, friend function, sizeof class with virtual, vfptr and vftable, vfptr and vftable using C, early binding and late binding, inheritance, virtual function call from a constructor/destructor, virtual destructor, virtual base class, virtual base class, exception, stack unwinding, exception handling, try-catch block, namespace, mutable variable and const function, mutable keyword, explicit, access a member function, object slicing, ctor sequence of constructor, dtor sequence of destructor, virtual destructor, print the type, dynamic casting, static casting, dynamic and static casting, const casting, reinterpret casting, static and reinterpret casting, template class, template vs macro,

#Object-Oriented Programming in C++ (4th Edition)
#Programming: Principles and Practice Using C++ (2nd Edition)
#The C++ Programming Language, 4th Edition
#Professional C++ Paperback by Nicholas A.Solter, Scott J.Kleper
#Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14