QMutex Class
The QMutex class provides access serialization between threads. More…
| Header | #include <QMutex> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QMutex() | |
| void | lock() |
| void | unlock() |
| bool | tryLock(int timeoutMs = 0) |
| bool | tryLock(QDeadlineTimer deadline) |
| bool | try_lock_for(std::chrono::duration<Rep, Period> duration) |
| bool | try_lock_until(std::chrono::time_point<Clock, Duration> timePoint) |
| bool | try_lock() |
| bool | isRecursive() const |
Detailed Description
A QMutex protects data that several threads access. A thread calls lock() before touching the data and unlock() afterwards; any other thread that calls lock() in the meantime blocks until the mutex is released. Prefer QMutexLocker, which unlocks the mutex automatically when it goes out of scope:
QMutex mutex;
int counter = 0;
void increment()
{
QMutexLocker locker(&mutex);
++counter;
}
A QMutex is not recursive: locking it twice from the same thread without unlocking in between is undefined behavior. Use QRecursiveMutex when that is needed.
See also QMutexLocker, QRecursiveMutex, QReadWriteLock and QWaitCondition.
Differences from Qt
QMutex wraps std::timed_mutex. It has no RecursionMode constructor argument.
Member Function Documentation
QMutex::QMutex()
Constructs an unlocked mutex.
bool QMutex::isRecursive() const
Returns false; a QMutex is never recursive.
See also QRecursiveMutex.
void QMutex::lock()
Locks the mutex. If another thread holds the lock, this call blocks until the mutex becomes available.
See also unlock() and tryLock().
bool QMutex::try_lock()
Attempts to lock the mutex without blocking. Returns true if the lock was obtained; otherwise returns false. This is provided for use with std::lock and std::unique_lock.
See also tryLock().
template <class Rep, class Period>bool QMutex::try_lock_for(std::chrono::duration<Rep, Period> duration)
Attempts to lock the mutex, waiting for at most duration. Returns true if the lock was obtained; otherwise returns false. This is provided for use with std::unique_lock.
See also tryLock().
template <class Clock, class Duration>bool QMutex::try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
Attempts to lock the mutex, waiting until timePoint at most. Returns true if the lock was obtained; otherwise returns false. This is provided for use with std::unique_lock.
See also tryLock().
bool QMutex::tryLock(int timeoutMs = 0)
Attempts to lock the mutex, waiting up to timeoutMs milliseconds for it to become available. Returns true if the lock was obtained; otherwise returns false.
If timeoutMs is 0, the call returns immediately. A negative timeoutMs waits until the lock is obtained, like lock().
bool QMutex::tryLock(QDeadlineTimer deadline)
This is an overloaded function.
Attempts to lock the mutex until deadline expires. Returns true if the lock was obtained; otherwise returns false. A deadline that never expires waits like lock().
This function was introduced in 6.6.
void QMutex::unlock()
Unlocks the mutex. The mutex must be locked by the calling thread.
See also lock().