互斥锁 QMutex Class 的翻译
互斥鎖 QMutex Class 的翻譯
簡介
互斥鎖的存在是為了保證線程間的訪問資源的連續(xù)性。該資源可以是一個對象(object),數(shù)據(jù)結(jié)構(gòu)(data structure),或者一段代碼( a section of code),并且保證同一時間只能有一個線程來訪問。
舉個列子:
int number = 6;void method1() {number *= 5;number /= 4; }void method2() {number *= 3;number /= 2; }如果,上述的方法在按次序訪問時候,那么結(jié)果的變化如下:
// 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如果,同時被兩個線程訪問,結(jié)果可能會變得糟糕:
// 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(); }這樣就可以保證只有一個線程在給定的時間內(nèi)使用,這樣就可以保證結(jié)果的準(zhǔn)確性。
如果線程1已經(jīng)ock(),線程2也嘗試lock(),那么線程2將阻塞,知道線程2解鎖為止。
使用方法:
Header: #include
qmake: QT += core
Inherits: QBasicMutex
Inherited By:
QRecursiveMutex
Public Types
enum RecursionMode { Recursive, NonRecursive }
Public Functions
QMutex(QMutex::RecursionMode mode)
QMutex()
~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()
成員介紹:
enum QMutex::RecursionMode
QMutex::Recursive 值1
在該模式下,一個線程可以加鎖多次,這個互斥關(guān)系將一直保持,知道所加鎖都被解鎖為止。
QMutex::NonRecursive 值 0
該模式下,只有一把鎖
函數(shù)成員:
1、
void QMutex::unlock()
解鎖。如果在去解鎖另外一個線程的鎖將會出錯。如果為上鎖,而去解鎖,也會出錯。
2、
bool QMutex::try_lock_until(std::chrono::time_point<Clock, Duration> timePoint)
嘗試上鎖。當(dāng)獲得鎖的時候,返回為真。反之,返回為負(fù)。
如果另外一個線程加了鎖,那么該線程會一直等到timePoint 時間點為止。
注意,調(diào)用該函數(shù)的時候,如果已經(jīng)過了該時間點,就等同于函數(shù)try_lock();
3、
bool QMutex::try_lock_for(std::chrono::duration<Rep, Period> duration)
嘗試上鎖。當(dāng)獲得鎖的時候,返回為真。反之,返回為負(fù)。
如果另外一個線程加了鎖,那么該線程會一直等,等待是時間間隔為duration;
注意:如果該時間間隔為負(fù),等同于函數(shù)try_lock();
4、
bool QMutex::try_lock()
嘗試上鎖。當(dāng)獲得鎖的時候,返回為真。反之,返回為負(fù)。
等同于tryLock();
5、
bool QMutex::tryLock(int timeout = 0)
嘗試上鎖。當(dāng)獲得鎖的時候,返回為真。反之,返回為負(fù)。
如果另一個線程已加鎖,那么該線程為等待(timeout 微妙)直到該鎖可以被該線程上鎖為止。
注意:如果timeout 為負(fù)數(shù) ,等同于lock(),那么表示會一直等,一直可以上鎖為止。
如果該線程上了鎖,那么只能有該線程來解鎖。
6、
void QMutex::lock()
上鎖。如果線程2已經(jīng)上鎖了,那么線程1會一直阻塞直到線程2解鎖為止。
7、
QMutex::QMutex(QMutex::RecursionMode mode)
創(chuàng)建鎖。默認(rèn)是未上鎖的狀態(tài)。
該函數(shù)表示可以上多次鎖。
8、
QMutex::QMutex()
創(chuàng)建鎖。默認(rèn)是未上鎖的狀態(tài)。默認(rèn)為QMutex::NonRecursive.
9、
QMutex::~QMutex()
銷毀鎖
10、
bool QMutex::isRecursive() const
返回是否是recursive. 模式。
本篇翻譯完畢。
總結(jié):
該類的描述還是很簡單清楚明了的。
本人也嘗試寫了一篇應(yīng)用,供讀者們參考:互斥鎖的簡單應(yīng)用
總結(jié)
以上是生活随笔為你收集整理的互斥锁 QMutex Class 的翻译的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Qt 互斥锁 QMutex 的简单应用
- 下一篇: 软设考试笔记--数据流图