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

(Last Updated On: May 18, 2010)
Learn the Secret

Get  our 2 Free Books

Get these now which land directly to their inbox.
Invalid email address

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!
This entry was posted in Quant Development and tagged , , , , , , , on by .

About caustic

Hi i there My name is Bryan Downing. I am part of a company called QuantLabs.Net This is specifically a company with a high profile blog about technology, trading, financial, investment, quant, etc. It posts things on how to do job interviews with large companies like Morgan Stanley, Bloomberg, Citibank, and IBM. It also posts different unique tips and tricks on Java, C++, or C programming. It posts about different techniques in learning about Matlab and building models or strategies. There is a lot here if you are into venturing into the financial world like quant or technical analysis. It also discusses the future generation of trading and programming Specialties: C++, Java, C#, Matlab, quant, models, strategies, technical analysis, linux, windows P.S. I have been known to be the worst typist. Do not be offended by it as I like to bang stuff out and put priorty of what I do over typing. Maybe one day I can get a full time copy editor to help out. Do note I prefer videos as they are much easier to produce so check out my many video at youtube.com/quantlabs