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;
}
/// 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;
}
To copy to clipboard, switch view to plain text mode
Bookmarks