Qt 4.8
|
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes. More...
#include <qmutex.h>
Public Functions | |
QMutex * | mutex () const |
Returns a pointer to the mutex that was locked in the constructor. More... | |
QMutexLocker (QMutex *m) | |
Constructs a QMutexLocker and locks mutex. More... | |
void | relock () |
Relocks an unlocked mutex locker. More... | |
void | unlock () |
Unlocks this mutex locker. More... | |
~QMutexLocker () | |
Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor. More... | |
Properties | |
quintptr | val |
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes.
Locking and unlocking a QMutex in complex functions and statements or in exception handling code is error-prone and difficult to debug. QMutexLocker can be used in such situations to ensure that the state of the mutex is always well-defined.
QMutexLocker should be created within a function where a QMutex needs to be locked. The mutex is locked when QMutexLocker is created. You can unlock and relock the mutex with unlock()
and relock()
. If locked, the mutex will be unlocked when the QMutexLocker is destroyed.
For example, this complex function locks a QMutex upon entering the function and unlocks the mutex at all the exit points:
This example function will get more complicated as it is developed, which increases the likelihood that errors will occur.
Using QMutexLocker greatly simplifies the code, and makes it more readable:
Now, the mutex will always be unlocked when the QMutexLocker object is destroyed (when the function returns since locker
is an auto variable).
The same principle applies to code that throws and catches exceptions. An exception that is not caught in the function that has locked the mutex has no way of unlocking the mutex before the exception is passed up the stack to the calling function.
QMutexLocker also provides a mutex()
member function that returns the mutex on which the QMutexLocker is operating. This is useful for code that needs access to the mutex, such as QWaitCondition::wait(). For example:
|
inlineexplicit |
Constructs a QMutexLocker and locks mutex.
The mutex will be unlocked when the QMutexLocker is destroyed. If mutex is zero, QMutexLocker does nothing.
Definition at line 104 of file qmutex.h.
|
inline |
Destroys the QMutexLocker and unlocks the mutex that was locked in the constructor.
Definition at line 115 of file qmutex.h.
|
inline |
Returns a pointer to the mutex that was locked in the constructor.
Definition at line 140 of file qmutex.h.
Referenced by QSemaphore::acquire(), QThreadPrivate::finish(), QThreadPoolThread::run(), QThread::start(), QSemaphore::tryAcquire(), QThread::wait(), and QThreadPoolPrivate::waitForDone().
|
inline |
Relocks an unlocked mutex locker.
Definition at line 125 of file qmutex.h.
Referenced by QMetaObject::activate(), QNetworkManagerEngine::activationFinished(), QNetworkManagerEngine::activeConnectionPropertiesChanged(), QIcdEngine::addConfiguration(), QConnmanEngine::addServiceConfiguration(), QIcdEngine::asyncUpdateConfigurationsSlot(), QConnmanEngine::configurationChange(), QIcdEngine::connectionStateSignalsSlot(), QNativeWifiEngine::connectToId(), QCoreWlanEngine::disconnectFromId(), QGenericEngine::doRequestUpdate(), QEventLoop::exec(), QThread::exec(), QThreadPrivate::finish(), QScanThread::foundNetwork(), QIcdEngine::icdServiceOwnerChanged(), initDefaultPaths(), QNetworkManagerEngine::initialize(), QIcdEngine::initialize(), QNetworkManagerEngine::interfacePropertiesChanged(), QCoreWlanEngine::networksChanged(), QNlaEngine::networksChanged(), QDeclarativePixmapReader::processJobs(), QDeclarativeXmlQueryEngine::processJobs(), QNetworkManagerEngine::removeAccessPoint(), QConnmanEngine::removeConfiguration(), QWindowsFileSystemWatcherEngine::removePaths(), QIcdEngine::requestUpdate(), QThreadPoolPrivate::reset(), QThreadPoolThread::run(), QWindowsFileSystemWatcherEngineThread::run(), QNativeWifiEngine::scanComplete(), QCoreApplicationPrivate::sendPostedEvents(), QThreadPoolPrivate::startFrontRunnable(), and QThread::~QThread().
|
inline |
Unlocks this mutex locker.
You can use relock()
to lock it again. It does not need to be locked when destroyed.
Definition at line 117 of file qmutex.h.
Referenced by QMetaObject::activate(), QNetworkManagerEngine::activationFinished(), QNetworkManagerEngine::activeConnectionPropertiesChanged(), QIcdEngine::addConfiguration(), QCoreApplication::addLibraryPath(), QConnmanEngine::addServiceConfiguration(), QIcdEngine::addSessionConfiguration(), QIcdEngine::asyncUpdateConfigurationsSlot(), QTextCodec::availableCodecs(), QTextCodec::availableMibs(), QConnmanEngine::configurationChange(), QIcdEngine::connectionStateSignalsSlot(), QCoreWlanEngine::connectToId(), QNativeWifiEngine::connectToId(), QIcdEngine::deleteConfiguration(), QUrl::detach(), QCoreWlanEngine::disconnectFromId(), QNativeWifiEngine::disconnectFromId(), QGenericEngine::doRequestUpdate(), QBBEngine::doRequestUpdate(), QCoreWlanEngine::doRequestUpdate(), QIcdEngine::doRequestUpdate(), QDeclarativeListModelWorkerAgent::event(), QEventLoop::exec(), QThread::exec(), QThreadPrivate::finish(), QScanThread::foundNetwork(), QIcdEngine::getIcdInitialState(), QDeclarativeEnginePrivate::getImageFromProvider(), QDeclarativeEnginePrivate::getImageProviderType(), QDeclarativeEnginePrivate::getPixmapFromProvider(), QIcdEngine::icdServiceOwnerChanged(), initDefaultPaths(), QNetworkManagerEngine::initialize(), QIcdEngine::initialize(), QNetworkManagerEngine::interfacePropertiesChanged(), QUrl::isParentOf(), QCoreWlanEngine::networksChanged(), QNlaEngine::networksChanged(), QNetworkManagerEngine::newAccessPoint(), QNetworkManagerEngine::newConnection(), QDBusConnectionPrivate::processFinishedCall(), QDeclarativePixmapReader::processJobs(), QDeclarativeXmlQueryEngine::processJobs(), QNetworkManagerEngine::removeAccessPoint(), QBBEngine::removeConfiguration(), QConnmanEngine::removeConfiguration(), QNetworkManagerEngine::removeConnection(), QWindowsFileSystemWatcherEngine::removePaths(), QPollingFileSystemWatcherEngine::removePaths(), QCoreApplication::removePostedEvents(), QNativeWifiEngine::requestUpdate(), QIcdEngine::requestUpdate(), QThreadPoolPrivate::reset(), QThreadPoolThread::run(), QWindowsFileSystemWatcherEngineThread::run(), QFileInfoGatherer::run(), QNativeWifiEngine::scanComplete(), QCoreApplicationPrivate::sendPostedEvents(), QCoreApplication::setLibraryPaths(), QThreadPoolPrivate::startFrontRunnable(), QNetworkSessionPrivateImpl::syncStateWithInterface(), QNetworkManagerEngine::updateAccessPoint(), QBBEngine::updateConfiguration(), QNetworkConfigurationManagerPrivate::updateConfigurations(), QNetworkManagerEngine::updateConnection(), QFileInfoGatherer::~QFileInfoGatherer(), and QThread::~QThread().