QMutex 类

The QMutex class provides access serialization between threads. 更多...

头: #include <QMutex>
qmake: QT += core
继承: QBasicMutex

注意: 此类的所有函数 thread-safe .

公共类型

enum RecursionMode { Recursive, NonRecursive }

公共函数

QMutex (RecursionMode mode = NonRecursive)
~QMutex ()
bool isRecursive () const
void lock ()
bool tryLock (int timeout = 0)
bool try_lock ()
bool try_lock_for (std::chrono::duration<Rep, Period> duration )
bool try_lock_until (std::chrono::time_point<Clock, Duration> timePoint )
void unlock ()

详细描述

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 关键词)。通常使用互斥最好采用 QMutexLocker 因为这使之能轻松确保锁定和解锁的一致履行。

例如,假定有方法每 2 行向用户打印消息:

int number = 6;
void method1()
{
    number *= 5;
    number /= 4;
}
void method2()
{
    number *= 3;
    number /= 2;
}
					

若连续调用这 2 方法,会发生以下:

// method1()
number *= 5;        // number is now 30
number /= 4;        // number is now 7
// method2()
number *= 3;        // number is now 21
number /= 2;        // number is now 10
					

若从 2 线程同时调用这 2 方法,就会产生以下序列:

// Thread 1 calls method1()
number *= 5;        // number is now 30
// Thread 2 calls method2().
//
// Most likely Thread 1 has been put to sleep by the operating
// system to allow Thread 2 to run.
number *= 3;        // number is now 90
number /= 2;        // number is now 45
// Thread 1 finishes executing.
number /= 4;        // number is now 11, instead of 10
					

若添加互斥,应该获得希望结果:

QMutex mutex;
int number = 6;
void method1()
{
    mutex.lock();
    number *= 5;
    number /= 4;
    mutex.unlock();
}
void method2()
{
    mutex.lock();
    number *= 3;
    number /= 2;
    mutex.unlock();
}
					

那么仅一线程可以修改 编号 在任何给定时间且结果是正确的。当然,这是通俗范例,但适用于事情需要按特定序列发生的任何其它情况。

当调用 lock () 在线程中,其它线程试着调用 lock () 在同一位置将阻塞,直到线程获得锁调用 unlock ()。非阻塞替代 lock () 是 tryLock ().

QMutex is optimized to be fast in the non-contended case. A non-recursive QMutex 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.

另请参阅 QMutexLocker , QReadWriteLock , QSemaphore ,和 QWaitCondition .

成员类型文档编制

enum QMutex:: RecursionMode

常量 描述
QMutex::Recursive 1 In this mode, a thread can lock the same mutex multiple times and the mutex won't be unlocked until a corresponding number of unlock () 调用已做出。
QMutex::NonRecursive 0 In this mode, a thread may only lock a mutex once.

另请参阅 QMutex ().

成员函数文档编制

QMutex:: QMutex ( RecursionMode mode = NonRecursive)

构造新的互斥。互斥是在解锁状态下创建的。

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. Otherwise a thread may only lock a mutex once. The default is QMutex::NonRecursive .

Recursive mutexes are slower and take more memory than non-recursive ones.

另请参阅 lock () 和 unlock ().

QMutex:: ~QMutex ()

销毁互斥。

警告: 销毁锁定互斥可能导致未定义行为。

bool QMutex:: isRecursive () const

返回 true if the mutex is recursive.

该函数在 Qt 5.7 引入。

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 递归互斥 . If this mutex is a non-recursive mutex , this function will dead-lock when the mutex is locked recursively.

另请参阅 unlock ().

bool QMutex:: tryLock ( int timeout = 0)

试图锁定互斥。此函数返回 true 若获得锁;否则它返回 false . If another thread has locked the mutex, this function will wait for at most timeout milliseconds for the mutex to become available.

注意:传递负数作为 timeout 相当于调用 lock (), i.e. this function will wait forever until mutex can be locked if timeout 为负。

If the lock was obtained, the mutex must be unlocked with unlock () 在另一线程可以成功锁定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 递归互斥 . If this mutex is a non-recursive mutex , this function will always return false when attempting to lock the mutex recursively.

另请参阅 lock () 和 unlock ().

bool QMutex:: try_lock ()

试图锁定互斥。此函数返回 true 若获得锁;否则它返回 false .

提供此函数是为兼容标准库概念 Lockable 。它相当于 tryLock ().

函数返回 true 若获得锁;否则它返回 false

该函数在 Qt 5.8 引入。

bool QMutex:: try_lock_for ( std::chrono::duration < Rep , Period > duration )

试图锁定互斥。此函数返回 true 若获得锁;否则它返回 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 相当于调用 try_lock (). This behavior differs from tryLock ().

If the lock was obtained, the mutex must be unlocked with unlock () 在另一线程可以成功锁定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 递归互斥 . If this mutex is a non-recursive mutex , this function will always return false when attempting to lock the mutex recursively.

该函数在 Qt 5.8 引入。

另请参阅 lock () 和 unlock ().

bool QMutex:: try_lock_until ( std::chrono::time_point < Clock , Duration > timePoint )

试图锁定互斥。此函数返回 true 若获得锁;否则它返回 false . If another thread has locked the mutex, this function will wait at least until timePoint for the mutex to become available.

注意:传递 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 () 在另一线程可以成功锁定它之前。

Calling this function multiple times on the same mutex from the same thread is allowed if this mutex is a 递归互斥 . If this mutex is a non-recursive mutex , this function will always return false when attempting to lock the mutex recursively.

该函数在 Qt 5.8 引入。

另请参阅 lock () 和 unlock ().

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.

另请参阅 lock ().