Qx v0.6
Qt Extensions Library
Loading...
Searching...
No Matches
Qx::ThreadSafeSingleton< Singleton, Mutex > Class Template Reference

The ThreadSafeSingleton template class provides access to a static singleton instance in a thread-safe manner. More...

#include <qx/core/qx-threadsafesingleton.h>

Static Public Member Functions

static Qx::ExclusiveAccess< Singleton, QMutexinstance ()
 

Protected Member Functions

 ThreadSafeSingleton ()=default
 

Detailed Description

template<class Singleton, typename Mutex = QMutex>
requires any_of<Mutex, QMutex, QRecursiveMutex>
class Qx::ThreadSafeSingleton< Singleton, Mutex >

This class allows one to easily utilize the singleton pattern while ensuring that access to the global shared instance remains thread-safe. This is achieved by providing basic scaffolding through a base class from which the actual singleton should derive. The instance() method provides a mutex protected pointer to the singleton instance via Qx::ExclusiveAccess. By declaring the final class' constructor as private, and accessing the class through instance(), one can be certain that the instance is only ever being used by one thread at a time.

If code in your singleton calls external code that in turn results in instance() being called again from within the same thread, be sure to use QRecursiveMutex when instantiating this template or else you may cause a dead-lock.

The following is a complete example for using Qx::ThreadSafeSingleton:

class MySingleton : Qx::ThreadSafeSingleton<MySingleton>
{
QX_THREAD_SAFE_SINGLETON(MySingleton);
private:
std::string mData;
MySingleton() = default; // Generally should be private
public:
doStuffSafely() { mData = "I'm for sure set while not being read!"; }
checkStuffSafely() { return mData; // Not being written to when returned }
}
//...
void functionInArbitraryThread()
{
auto singleton = MySingleton::instance();
// This function now has a exclusive access to MySingleton (i.e. a mutex lock is established)
singleton->doStuffSafely();
// Unlocked when 'singleton' goes out of scope, or is manually unlocked.
}
void functionInAnotherThread()
{
// Safely lock and read. It's guarenteed that no other thread is using MySingleton
// after the instance is obtained.
auto singleton = MySingleton::instance();
std::string info = singleton->checkStuffSafely();
//...
}
The ThreadSafeSingleton template class provides access to a static singleton instance in a thread-saf...
Definition qx-threadsafesingleton.h:20

Constructor & Destructor Documentation

◆ ThreadSafeSingleton()

template<class Singleton , typename Mutex = QMutex>
Qx::ThreadSafeSingleton< Singleton, Mutex >::ThreadSafeSingleton ( )
protecteddefault

Constructs a ThreadSafeSingleton.

See also
instance().

Member Function Documentation

◆ instance()

template<class Singleton , typename Mutex = QMutex>
Qx::ExclusiveAccess< Singleton, QMutex > Qx::ThreadSafeSingleton< Singleton, Mutex >::instance ( )
inlinestatic

Returns a handle to the singleton instance.


The documentation for this class was generated from the following files: