Qt 4.8
|
The QMutex class provides access serialization between threads. More...
#include <qmutex.h>
Public Types | |
enum | RecursionMode { NonRecursive, Recursive } |
Public Functions | |
void | lock () |
Locks the mutex. More... | |
void | lockInline () |
QMutex (RecursionMode mode=NonRecursive) | |
Constructs a new mutex. More... | |
bool | tryLock () |
Attempts to lock the mutex. More... | |
bool | tryLock (int timeout) |
Attempts to lock the mutex. More... | |
bool | tryLockInline () |
void | unlock () |
Unlocks the mutex. More... | |
void | unlockInline () |
~QMutex () | |
Destroys the mutex. More... | |
Private Functions | |
void | lockInternal () |
void | unlockInternal () |
Properties | |
QMutexData * | d |
Friends | |
class | QWaitCondition |
class | QWaitConditionPrivate |
The QMutex class provides access serialization between threads.
The purpose of a QMutex is to protect an object, data structure or section of code so that only one thread can access it at a time (this is similar to the Java synchronized
keyword). It is usually best to use a mutex with a QMutexLocker since this makes it easy to ensure that locking and unlocking are performed consistently.
For example, say there is a method that prints a message to the user on two lines:
If these two methods are called in succession, the following happens:
If these two methods are called simultaneously from two threads then the following sequence could result:
If we add a mutex, we should get the result we want:
Then only one thread can modify number
at any given time and the result is correct. This is a trivial example, of course, but applies to any other case where things need to happen in a particular sequence.
When you call lock() in a thread, other threads that try to call lock() in the same place will block until the thread that got the lock calls unlock(). A non-blocking alternative to lock() is tryLock().
Enumerator | |
---|---|
NonRecursive | |
Recursive |
Definition at line 66 of file qmutex.h.
|
explicit |
Constructs a new mutex.
Use the constructor that takes a RecursionMode parameter instead.
The mutex is created in an unlocked state.
If mode is QMutex::Recursive, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of unlock() calls have been made. The default is QMutex::NonRecursive.
Definition at line 127 of file qmutex.cpp.
QMutex::~QMutex | ( | ) |
Destroys the mutex.
Definition at line 136 of file qmutex.cpp.
void QMutex::lock | ( | ) |
Locks the mutex.
If another thread has locked the mutex then this call will block until that thread has unlocked it.
Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will dead-lock when the mutex is locked recursively.
Definition at line 151 of file qmutex.cpp.
Referenced by QNetworkManagerEngine::activationFinished(), QNetworkManagerEngine::activeConnectionPropertiesChanged(), QIcdEngine::addConfiguration(), QNetworkConfigurationManagerPrivate::configurationAdded(), QConnmanEngine::configurationChange(), QNetworkConfigurationManagerPrivate::configurationChanged(), QNetworkConfigurationManagerPrivate::configurationRemoved(), QNetworkManagerEngine::deviceAdded(), QNetworkSessionPrivateImpl::do_open(), QGenericEngine::doRequestUpdate(), QIcdEngine::doRequestUpdate(), QNlaThread::fetchConfigurations(), QThreadPrivate::finish(), QNlaThread::forceUpdate(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QIcdEngine::getIcdInitialState(), QNetworkSessionPrivateImpl::getStatistics(), QDeclarativeXmlQueryEngine::instance(), QNetworkManagerEngine::interfacePropertiesChanged(), QDeclarativePixmap::load(), QFreetypeFace::lock(), QWSDirectPainterSurface::lock(), locking_function(), QDeclarativePixmapReader::networkRequestDone(), QCoreWlanEngine::networksChanged(), QNlaEngine::networksChanged(), QNetworkManagerEngine::newAccessPoint(), QNetworkManagerEngine::parseConnection(), QWaitConditionPrivate::post(), QCoreApplication::postEvent(), QDBusMutexLocker::QDBusMutexLocker(), QDeclarativeWorkerScriptEngine::QDeclarativeWorkerScriptEngine(), qt_adopted_thread_watcher_function(), QDeclarativePixmapData::release(), QOrderedMutexLocker::relock(), QNetworkManagerEngine::removeAccessPoint(), QScanThread::run(), QWindowsPipeWriter::run(), QDeclarativeXmlQueryEngine::run(), QNlaThread::run(), QNativeWifiEngine::scanComplete(), QDnotifySignalThread::startNotify(), QNetworkSessionPrivateImpl::stateChange(), QFontEngineFT::stringToCMap(), QDeclarativeListModelWorkerAgent::sync(), QNetworkManagerEngine::updateAccessPoint(), QNlaThread::updateConfigurations(), QNetworkManagerEngine::updateConnection(), QNetworkSessionPrivateImpl::updateIdentifier(), QNetworkSessionPrivateImpl::updateState(), QWaitCondition::wait(), QDBusConnectionPrivate::waitForFinished(), QDeclarativePixmapReader::~QDeclarativePixmapReader(), QDeclarativeWorkerScriptEngine::~QDeclarativeWorkerScriptEngine(), QDeclarativeXmlQueryEngine::~QDeclarativeXmlQueryEngine(), QNlaThread::~QNlaThread(), and QWindowsPipeWriter::~QWindowsPipeWriter().
|
inline |
Definition at line 198 of file qmutex.h.
Referenced by QMutexLocker::QMutexLocker(), and QOrderedMutexLocker::relock().
|
private |
Definition at line 454 of file qmutex.cpp.
Referenced by lock().
bool QMutex::tryLock | ( | ) |
Attempts to lock the mutex.
If the lock was obtained, this function returns true. If another thread has locked the mutex, this function returns false immediately.
If the lock was obtained, the mutex must be unlocked with unlock() before another thread can successfully lock it.
Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will always return false when attempting to lock the mutex recursively.
Definition at line 201 of file qmutex.cpp.
bool QMutex::tryLock | ( | int | timeout | ) |
Attempts to lock the mutex.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function returns true if the lock was obtained; otherwise it returns false. If another thread has locked the mutex, this function will wait for at most timeout milliseconds for the mutex to become available.
Note: Passing a negative number as the timeout is equivalent to calling lock(), i.e. this function will wait forever until mutex can be locked if timeout is negative.
If the lock was obtained, the mutex must be unlocked with unlock() before another thread can successfully lock it.
Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a recursive mutex. If this mutex is a non-recursive mutex, this function will always return false when attempting to lock the mutex recursively.
Definition at line 256 of file qmutex.cpp.
|
inline |
Definition at line 197 of file qmutex.h.
Referenced by QOrderedMutexLocker::relock().
void QMutex::unlock | ( | ) |
Unlocks the mutex.
Attempting to unlock a mutex in a different thread to the one that locked it results in an error. Unlocking a mutex that is not locked results in undefined behavior.
Definition at line 296 of file qmutex.cpp.
Referenced by QNetworkManagerEngine::activationFinished(), QNetworkManagerEngine::activeConnectionPropertiesChanged(), QIcdEngine::addConfiguration(), QNetworkConfigurationManagerPrivate::configurationAdded(), QConnmanEngine::configurationChange(), QNetworkConfigurationManagerPrivate::configurationChanged(), QNetworkConfigurationManagerPrivate::configurationRemoved(), QNetworkManagerEngine::deviceAdded(), QNetworkSessionPrivateImpl::do_open(), QGenericEngine::doRequestUpdate(), QIcdEngine::doRequestUpdate(), QNlaThread::fetchConfigurations(), QThreadPrivate::finish(), QNlaThread::forceUpdate(), QAudioOutputPrivate::freeBlocks(), QAudioInputPrivate::freeBlocks(), QIcdEngine::getIcdInitialState(), QNetworkSessionPrivateImpl::getStatistics(), QDeclarativeXmlQueryEngine::instance(), QNetworkManagerEngine::interfacePropertiesChanged(), QDeclarativePixmap::load(), locking_function(), QDeclarativePixmapReader::networkRequestDone(), QCoreWlanEngine::networksChanged(), QNlaEngine::networksChanged(), QNetworkManagerEngine::newAccessPoint(), QNetworkManagerEngine::parseConnection(), QWaitConditionPrivate::post(), QCoreApplication::postEvent(), QDeclarativeWorkerScriptEngine::QDeclarativeWorkerScriptEngine(), qt_adopted_thread_watcher_function(), QDeclarativePixmapData::release(), QOrderedMutexLocker::relock(), QNetworkManagerEngine::removeAccessPoint(), QScanThread::run(), QWindowsPipeWriter::run(), QDeclarativeXmlQueryEngine::run(), QNlaThread::run(), QNativeWifiEngine::scanComplete(), QDnotifySignalThread::startNotify(), QNetworkSessionPrivateImpl::stateChange(), QFontEngineFT::stringToCMap(), QDeclarativeListModelWorkerAgent::sync(), QFreetypeFace::unlock(), QMutexUnlocker::unlock(), QWSDirectPainterSurface::unlock(), QNetworkManagerEngine::updateAccessPoint(), QNlaThread::updateConfigurations(), QNetworkManagerEngine::updateConnection(), QNetworkSessionPrivateImpl::updateIdentifier(), QNetworkSessionPrivateImpl::updateState(), QWaitCondition::wait(), QDBusConnectionPrivate::waitForFinished(), QDBusMutexLocker::~QDBusMutexLocker(), QDeclarativePixmapReader::~QDeclarativePixmapReader(), QDeclarativeWorkerScriptEngine::~QDeclarativeWorkerScriptEngine(), QDeclarativeXmlQueryEngine::~QDeclarativeXmlQueryEngine(), QNlaThread::~QNlaThread(), and QWindowsPipeWriter::~QWindowsPipeWriter().
|
inline |
Definition at line 196 of file qmutex.h.
Referenced by QMetaObjectPrivate::disconnectHelper(), QOrderedMutexLocker::unlock(), and QObject::~QObject().
|
private |
Definition at line 513 of file qmutex.cpp.
|
friend |
|
friend |
|
private |
Definition at line 98 of file qmutex.h.
Referenced by lock(), lockInternal(), QWaitConditionPrivate::post(), tryLock(), unlock(), unlockInternal(), QWaitCondition::wait(), and ~QMutex().