Tag Archives: Singleton

A C++ multi thread safe singleton example with source code has been posted

Lock.h

//Example from http://www.relisoft.com/Win32/active.html
#if !defined _LOCK_H_
#define _LOCK_H_

#include “Mutex.h”

class Lock
{
public:
// Acquire the state of the semaphore
Lock ( Mutex & mutex )
: _mutex(mutex)
{
_mutex.Acquire();
}
// Release the state of the semaphore
~Lock ()
{
_mutex.Release();
}
private:
Mutex & _mutex;
};

#endif

Mutex.h

//Example from http://www.relisoft.com/Win32/active.html
#if !defined _MUTEX_H_
#define _MUTEX_H_

class Mutex
{
friend class Lock;
public:
Mutex () { InitializeCriticalSection (& _critSection); }
~Mutex () { DeleteCriticalSection (& _critSection); }
private:
void Acquire ()
{
EnterCriticalSection (& _critSection);
}
void Release ()
{
LeaveCriticalSection (& _critSection);
}

CRITICAL_SECTION _critSection;
};

#endif

Singleton.h

#include “Lock.h”
#include “Mutex.h”

class aSingletonClass
{
public:
//method to get Instance of class
static aSingletonClass *getInstance( void )
{
//Note that the class is only created when this method is called first time
if(!instance_)
instance_ = new aSingletonClass;
return instance_;
}
//method to delete Instance of class
static void deleteInstance( void )
{
if(instance_)
delete instance_;
instance_ = NULL; //important as this can create dead reference problems
}
void printSomething(char *name, int count)
{
Lock guard(mutex_);
std::cout << name << " loop " << count << std::endl; } private: //variable to store the instance of singleton static aSingletonClass *instance_; //default constructor should be private to prevent instantiation aSingletonClass() {}; //destructor should be made private so no one can delete this accidently ~aSingletonClass() {}; //We also need to prevent copy being created of the object aSingletonClass(const aSingletonClass&); Mutex mutex_; }; Thread.cpp //Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy //This shows example of Multithreading, thread sync, Mutex #include
#include #include
#include “Singleton.h”

using namespace std;

aSingletonClass* aSingletonClass::instance_ = NULL;

void Func1(void *);
void Func2(void *);

int main()
{
HANDLE hThreads[2];

aSingletonClass *someVar = NULL;
//Create Instance
someVar = aSingletonClass::getInstance();

//Create two threads and start them
hThreads[0] = (HANDLE)_beginthread(Func1, 0, NULL);
hThreads[1] = (HANDLE)_beginthread(Func2, 0, NULL);

//Makes sure that both the threads have finished before going further
WaitForMultipleObjects(2, hThreads, TRUE, INFINITE);

cout << "Main exit" << endl; return 0; } void Func1(void *P) { int Count; for (Count = 1; Count < 11; Count++) { aSingletonClass::getInstance()->printSomething(“Func1”, Count);
}
return;
}

void Func2(void *P)
{
int Count;

for (Count = 10; Count > 0; Count–)
{
aSingletonClass::getInstance()->printSomething(“Func2”, Count);
}
return;
}

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C# or Java Job Interview Question: Singleton design pattern source code sample which is thread safe

C# or Java Job Interview Question: Singleton design pattern source code sample which is thread safe

Below is a sample but we assume the Mutex locks are already defined. See my previous posts on multithreading for an example how to do to this.

#include
using namespace std;

template
class Lock
{
public:
Lock( T& t )
: t_(t)
, locked_(true)
{
t_.Lock();
}

~Lock()
{
Unlock();
}

void Unlock()
{
if( locked_ )
{
t_.Unlock();
locked_ = false;
}
}

private:
T& t_;
bool locked_;
};

template
class Singleton
{
public:
T* Instance()
{
if(!instance)
{

//use pthread_mutex_lock?
Lock l(m);
//Lock mutex; //-+

if(!instance) // Double check
{
instance = new T;
}
}

return instance;
}
private:
Singleton(){}
~Singleton(){}
static T *instance;
};

int main() {
return 0;
}

For C# with no multi threading:
Implement singleton
In C#:
public sealed class Singleton //sealed prevents class from derived so like final in Java
{
private static Singleton s_instance = new Singleton();
// Make the constructor private so that no new instance can be generated from outside
private Singleton() {}
public Singleton GetInstance()
{
return s_instance();
}

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

C++ Job Interview Question: Singleton design pattern source code sample which is thread safe

C++ Job Interview Question: Singleton design pattern source code sample which is thread safe
Below is a sample but we assume the Mutex locks are already defined. See my previous posts on multithreading for an example how to do to this.

#include
using namespace std;

template
class Lock
{
public:
Lock( T& t )
: t_(t)
, locked_(true)
{
t_.Lock();
}

~Lock()
{
Unlock();
}

void Unlock()
{
if( locked_ )
{
t_.Unlock();
locked_ = false;
}
}

private:
T& t_;
bool locked_;
};

template
class Singleton
{
public:
T* Instance()
{
if(!instance)
{

//use pthread_mutex_lock?
Lock l(m);
//Lock mutex; //-+

if(!instance) // Double check
{
instance = new T;
}
}

return instance;
}
private:
Singleton(){}
~Singleton(){}
static T *instance;
};

int main() {
return 0;
}

C++ Job Interview Question: Singleton design pattern source code sample which is thread safe
Below is a sample but we assume the Mutex locks are already defined. See my previous posts on multithreading for an example how to do to this.

#include
using namespace std;

template
class Lock
{
public:
Lock( T& t )
: t_(t)
, locked_(true)
{
t_.Lock();
}

~Lock()
{
Unlock();
}

void Unlock()
{
if( locked_ )
{
t_.Unlock();
locked_ = false;
}
}

private:
T& t_;
bool locked_;
};

template
class Singleton
{
public:
T* Instance()
{
if(!instance)
{

//use pthread_mutex_lock?
Lock l(m);
//Lock mutex; //-+

if(!instance) // Double check
{
instance = new T;
}
}

return instance;
}
private:
Singleton(){}
~Singleton(){}
static T *instance;
};

int main() {
return 0;
}

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!