Tag Archives: thread safe

Youtube video on Intro to c++ Boost Signals for Thread Safe Even Handling with Callback Capability

Youtube video on Intro to c++ Boost Signals for Thread Safe Even Handling with Callback Capaibility

–> JOIN NOW BEFORE THE QUANTLABS.NET PREMIUM MEMBERSHIP GO UP 50% <–

Join my FREE newsletter to see how I develop my custom automated trading system 

 

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!

Matlab Compiler generated DLLs are not thread safe!!! Even for C# or C++ shared libraries

Matlab Compiler generated DLLs are not thread safe!!! Even for C# or C++ shared libraries

Loren,

What about thread safety? Can I call the functions in the shared library from multiple threads?
Thanks,

….

Tom,

You can call the libraries from multiple threads, but only one thread can be active in a library at any time. The generated libraries are protected by semaphores, which only allow one user thread into the generated library at any one time. Other user threads that try to call into the shared library will block (wait) until the shared library is “free”.

 

http://blogs.mathworks.com/loren/2011/02/03/creating-c-shared-libraries-and-dlls/

So what are the alternatives? Find out what I plan to do by joining my free newsletter!!

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!