DigestCPP

Lets Understand With Example

  • Home
  • Design Patterns
  • Adapter Design Pattern
  • Strategy Design Pattern
  • C++11 Features
  • std::thread c++11
  • Contact Us

Singleton Thread Safe Code Example

Return Back to description

Code with Example:

/*
* File:singletonLogger.cpp
* This file is described the Singleton Design Pattern with help of example
* Singleton Design pattern:Ensure ONLY one object will be created.
* Singleton Design pattern:Provide Globall access to it.
* Author: Aplha
* Date: 02 Sep 2020
*/

///////////////////////////////////Header files////////////////////////////////////
#include <iostream>
#include <mutex>
#include <thread>

//////////////////////////////////Singleton Start//////////////////////////////////////

class SingletonLogger
{

private:
static std::mutex m_mutex;
static SingletonLogger* m_pinstance;

//Singleton's constructor should be private to prevent creation of direct object.
SingletonLogger(){}
~SingletonLogger() {}

SingletonLogger(const SingletonLogger &other) = delete; // Disallow copying
void operator=(const SingletonLogger &) = delete; // Disallow copying

public:

static SingletonLogger *GetInstance();
void Print(const std::string str);

};

// Static variale should be defined outside the class.
SingletonLogger* SingletonLogger::m_pinstance{nullptr};
std::mutex SingletonLogger::m_mutex;

//Get instance with Double Checked Locking Pattern
//You can see two if conditions are there with one lock
SingletonLogger *SingletonLogger::GetInstance()
{
if (m_pinstance == nullptr)
{
std::lock_guard<std::mutex> lock(m_mutex);
if (m_pinstance == nullptr)
{
m_pinstance = new SingletonLogger();
}
}
return m_pinstance;
}

//Print Function of Logger, this is basic function to explain the use of singleton Pattern
// we can add according to our requirement.
void SingletonLogger::Print(const std::string str)
{
std::lock_guard<std::mutex> lock(m_mutex);
std::cout<<"object Address:"<<m_pinstance<<" value:"<<str<<std::endl;
}

///////////////////////////////////////////////Singleton END////////////////////////////////

void ThreadOne(){
// Thread One
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
SingletonLogger::GetInstance()->Print("Thread ONE is executing");
}

void ThreadTwo(){
// Thread Two
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
SingletonLogger::GetInstance()->Print("Thread TWO is executing");
}

int main()
{
std::thread t1(ThreadOne);
std::thread t2(ThreadTwo);
//waiting
SingletonLogger::GetInstance()->Print("Parent process is Waiting for threads");
t1.join();
t2.join();
SingletonLogger::GetInstance()->Print("Parent process please check the address of singleton object it must be same");
SingletonLogger::GetInstance()->Print("Parent process saying BYE BYE");
return 0;
}

Compilation command:

g++ -pthread -std=c++11 singletonLogger.cpp

Output:

/DesignPattern$ ./a.out 
object Address:0x1ed8ef0 value:Parent process is Waiting for threads
object Address:0x1ed8ef0 value:Thread ONE is executing
object Address:0x1ed8ef0 value:Thread TWO is executing
object Address:0x1ed8ef0 value:Parent process please check the address of singleton object it must be same
object Address:0x1ed8ef0 value:Parent process saying BYE BYE

Return Back to description

Primary Sidebar

Copyright © 2021

  • Contact us