Qx v0.6
Qt Extensions Library
|
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, QMutex > | instance () |
Protected Member Functions | |
ThreadSafeSingleton ()=default | |
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:
|
protecteddefault |
Constructs a ThreadSafeSingleton.
|
inlinestatic |
Returns a handle to the singleton instance.