Qt中互斥鎖QMutex和QMutexLocker的使用
QMutex和QMutexLocker
類(lèi) QMutex 的主要函數(shù)有:
- lock (); 加鎖,如果該互斥鎖被占用,該函數(shù)阻塞,直到互斥鎖被釋放。
- unlock ();
- 解鎖bool tryLock (int timeout = 0);
表示嘗試去加鎖,timeout 為超時(shí)時(shí)間。如果互斥鎖為可用狀態(tài),該函數(shù)會(huì)占用該互斥鎖,并返回 true ,否則返回 false 。如果互斥鎖被另一個(gè)線(xiàn)程占用,該函數(shù)會(huì)等待 timeout 毫秒直到互斥鎖為可用狀態(tài)。
QMutexLocker 類(lèi)的主要作用是用來(lái)管理 QMutex使用 QMutexLocker 的好處是,可以防止線(xiàn)程死鎖。該對(duì)象在構(gòu)造的時(shí)候加鎖,析構(gòu)的時(shí)候解鎖。
使用場(chǎng)景
QMutex目的是保護(hù)一次只有一個(gè)線(xiàn)程訪(fǎng)問(wèn)一個(gè)對(duì)象、數(shù)據(jù)結(jié)構(gòu)或一段代碼。QMutex通常在較為簡(jiǎn)單的代碼中使用,如果代碼復(fù)雜最好使用【QMutexLocker+互斥鎖】進(jìn)行多線(xiàn)程同步,這樣可以很容易確保鎖定和解鎖操作執(zhí)行一致。
? - 在復(fù)雜函數(shù)和語(yǔ)句或異常處理代碼中l(wèi)ock和unlock QMutex很容易出錯(cuò),而且很難調(diào)試。在這種情況下,可以使用QMutexLocker替代。
- ? QMutexLocker在一個(gè)需要鎖定QMutex的函數(shù)中創(chuàng)建。當(dāng)創(chuàng)建QMutexLocker時(shí),互斥鎖被鎖定(后面可以使用unlock()和relock()對(duì)互斥鎖進(jìn)行解鎖和重新鎖定)。如果互斥鎖鎖定了,互斥對(duì)象將在QMutexLocker銷(xiāo)毀時(shí)被解鎖。
- 即QMutexLocker創(chuàng)建時(shí)鎖定,銷(xiāo)毀時(shí)解鎖,所以一般為局部變量
QMutex
預(yù)期:兩個(gè)線(xiàn)程使用一把鎖,操作一個(gè)數(shù)據(jù),數(shù)據(jù)會(huì)被兩個(gè)線(xiàn)程依次打印1.2.3.4…
MyThread.h
#ifndef MYTHREAD_H #define MYTHREAD_H #include <QThread> #include <QMutex> class MyThread : public QThread { Q_OBJECT public: explicit MyThread(QObject *parent = nullptr); void run(); private: static QMutex mutex; // 多個(gè)線(xiàn)程使用一把鎖 static int num; // 多個(gè)線(xiàn)程訪(fǎng)問(wèn)一個(gè)數(shù)據(jù) }; #endif // MYTHREAD_H
MyThread.cpp
#include "mythread.h" #include <QDebug> QMutex MyThread::mutex; int MyThread::num = 0; MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { while (1) { this->mutex.lock();// 加鎖 qDebug() << "Current Thread: " << this << ", Value: " << this->num++; this->mutex.unlock();// 解鎖 QThread::sleep(1);// 線(xiàn)程睡眠兩秒 } }
mainWindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QPainter> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private: Ui::MainWindow *ui; }; #endif // MAINWINDOW_H
mainWindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include "mythread.h" #include <QDebug> #include <QLabel> #include <QFileInfo> #include <QPushButton> #include <QHBoxLayout> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); MyThread *myThread1 = new MyThread(this); MyThread *myThread2 = new MyThread(this); myThread1->start(); myThread2->start(); } MainWindow::~MainWindow() { delete ui; }
運(yùn)行結(jié)果:
QMutexLocker
預(yù)期:兩個(gè)線(xiàn)程使用一把鎖,操作一個(gè)數(shù)據(jù),數(shù)據(jù)會(huì)被兩個(gè)線(xiàn)程依次打印1.2.3.4…
這里只改變了MyThread.cpp
MyThread.cpp
#include "mythread.h" #include <QDebug> #include <QMutexLocker> QMutex MyThread::mutex; int MyThread::num = 0; MyThread::MyThread(QObject *parent) : QThread(parent) { } void MyThread::run() { while (1) { // QMutexLocker:創(chuàng)建的時(shí)候加鎖,當(dāng)QMutexLocker局部銷(xiāo)毀的時(shí)候解鎖 { QMutexLocker lock(&this->mutex); qDebug() << "Current Thread: " << this << ", Value: " << this->num++; } QThread::sleep(1);// 線(xiàn)程睡眠兩秒 } }
運(yùn)行結(jié)果:
到此這篇關(guān)于Qt中互斥鎖QMutex和QMutexLocker的使用的文章就介紹到這了,更多相關(guān)Qt 互斥鎖 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?vector的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了C++?vector的簡(jiǎn)單實(shí)現(xiàn),使用數(shù)據(jù)庫(kù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2022-03-03C++中拷貝構(gòu)造函數(shù)的應(yīng)用詳解
這篇文章主要介紹了C++中拷貝構(gòu)造函數(shù)的應(yīng)用,需要的朋友可以參考下2014-07-07strcpy函數(shù)實(shí)現(xiàn)簡(jiǎn)示例命分享
這篇文章主要介紹了strcpy函數(shù)實(shí)現(xiàn)簡(jiǎn)示例命,需要的朋友可以參考下2014-03-03C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單回聲服務(wù)器
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單回聲服務(wù)器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存
這篇文章主要介紹了C++之實(shí)現(xiàn)快速清空vector以及釋放vector內(nèi)存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08C語(yǔ)言實(shí)現(xiàn)模擬USB對(duì)8bit數(shù)據(jù)的NRZI編碼輸出
今天小編就為大家分享一篇關(guān)于C語(yǔ)言實(shí)現(xiàn)模擬USB對(duì)8bit數(shù)據(jù)的NRZI編碼輸出,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12C++實(shí)現(xiàn)漢諾塔算法經(jīng)典實(shí)例
這篇文章主要介紹了C++實(shí)現(xiàn)漢諾塔算法經(jīng)典實(shí)例,代碼簡(jiǎn)潔高效,對(duì)于學(xué)習(xí)算法的朋友有一定的借鑒價(jià)值,需要的朋友可以參考下2014-07-07