#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.

Most popular topics (must visit)

Useful links for students