PDA

View Full Version : QThread call-once semantics?



brcain
20th October 2006, 01:27
I'm relatively new to QThread and am changing some code that previously used Boost threads.

Does QThread support the call-once semantics ... similar to Boost code below?



/// Description: Thread-safe singleton class.

// Code in header file ...
class cSingleton
{
public:
static cSingleton * getInstance();
private:
cSingleton() {}
static void createInstance(); // This method invoked by boost::call_once
static boost::once_flag mgOnceFlag; // Special flag used to check if associated
// boost::call_once routine has been called yet
static cSingleton * mgInstance; // Singleton instance
};


// Code in source file ...
cSingleton * cSingleton::mgInstance(0);

// Statically intialized at compile time ... thus not subject to multithreaded
// initialization problems.
boost::once_flag cSingleton::mgOnceFlag(BOOST_ONCE_INIT);

cSingleton * cSingleton::getInstance()
{
// boost::call_once is necesary for thread-safetly because multiple threads
// can call this method at the same time ... creating two instances of the
// singleton. Using boost::call_once(), only one thread is actually able to
// call createInstance().
boost::call_once(&cSingleton::createInstance, mgOnceFlag);
return mgInstance;
}

void cSingleton::createInstance()
{
if(!mgInstance)
mgInstance = new cSingleton;
}

danadam
20th October 2006, 03:49
Well, it's about access serialization, so I would use QMutex:

// Singleton.h
#include <QMutex>

class Singleton
{
public:
static Singleton *getInstance();

private:
Singleton();

static Singleton *instance;
static QMutex instanceMutex;
};

// Singleton.cpp
#include <QtDebug>
#include <QMutexLocker>

Singleton *Singleton::instance(0);
QMutex Singleton::instanceMutex;

Singleton *Singleton::getInstance()
{
if (!instance) {
QMutexLocker instanceMutexLocker(&instanceMutex);
if (!instance) {
instance = new Singleton();
}
}

return instance;
}

Singleton::Singleton()
{
qDebug() << "Singleton ctor()";
}

brcain
20th October 2006, 17:02
Well, it's about access serialization, so I would use QMutex

Yep. I knew I could do that for this case. Maybe call-once semantics is unnecessary with the mutex. I wonder why Boost thought it necessary ... other cases that it might address.

Do you know if Qt has something similar ... just as a mental note?

danadam
20th October 2006, 21:22
I wonder why Boost thought it necessary ... other cases that it might address.
Unfortunately I'm not very much familiar with boost, and I cannot think up any reason.


Do you know if Qt has something similar
I've never come across anything similar, but I've been dealing with Qt for only a few months, so it doesn't mean that no such thing exists.

jacek
20th October 2006, 21:27
Do you know if Qt has something similar ... just as a mental note?
You can construct it yourself using QMutex or QSemaphore.

brcain
20th October 2006, 22:25
You can construct it yourself using QMutex or QSemaphore.

Yep ... did that ;)

Thanks