![]() |
Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
|
\inmodule QtCore More...
#include <qmutex.h>
Public Member Functions | |
constexpr | QMutex () noexcept |
Constructs a new mutex. | |
void | lock () noexcept |
Locks the mutex. | |
bool | tryLock (int timeout=0) noexcept |
Attempts to lock the mutex. | |
bool | try_lock () noexcept |
void | unlock () noexcept |
Unlocks the mutex. | |
template<class Rep, class Period> | |
bool | try_lock_for (std::chrono::duration< Rep, Period > duration) noexcept |
template<class Clock, class Duration> | |
bool | try_lock_until (std::chrono::time_point< Clock, Duration > timePoint) noexcept |
\inmodule QtCore
The QMutex class provides access serialization between threads.
\threadsafe
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().
QMutex is optimized to be fast in the non-contended case. It will not allocate memory if there is no contention on that mutex. It is constructed and destroyed with almost no overhead, which means it is fine to have many mutexes as part of other classes.
|
inlineconstexprnoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
|
inlinenoexcept |
Attempts to lock the mutex. 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 least duration for the mutex to become available.
Note: Passing a negative duration as the duration is equivalent to calling try_lock(). This behavior differs from tryLock().
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 will cause a dead-lock.
|
inlinenoexcept |
Attempts to lock the mutex. This function returns true
if the lock was obtained; otherwise it returns false
. If another thread has locked the mutex, this function will wait at least until timePoint for the mutex to become available.
Note: Passing a timePoint which has already passed is equivalent to calling try_lock(). This behavior differs from tryLock().
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 will cause a dead-lock.
|
inlinenoexcept |
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.
Attempts to lock the mutex.
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 will cause a dead-lock.
Attempts to lock the mutex. This function returns true
if the lock was obtained; otherwise it returns false
. If another thread has locked the mutex, this function will wait until timer expires for the mutex to become available.
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 will cause a dead-lock.
This function returns true
if the lock was obtained; otherwise it returns false
.
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 will cause a dead-lock.
|
inlinenoexcept |