Qt中互斥鎖QMutex和QMutexLocker的使用
QMutex和QMutexLocker
類 QMutex 的主要函數(shù)有:
- lock (); 加鎖,如果該互斥鎖被占用,該函數(shù)阻塞,直到互斥鎖被釋放。
- unlock ();
- 解鎖bool tryLock (int timeout = 0);
表示嘗試去加鎖,timeout 為超時時間。如果互斥鎖為可用狀態(tài),該函數(shù)會占用該互斥鎖,并返回 true ,否則返回 false 。如果互斥鎖被另一個線程占用,該函數(shù)會等待 timeout 毫秒直到互斥鎖為可用狀態(tài)。
QMutexLocker 類的主要作用是用來管理 QMutex使用 QMutexLocker 的好處是,可以防止線程死鎖。該對象在構(gòu)造的時候加鎖,析構(gòu)的時候解鎖。
使用場景
QMutex目的是保護(hù)一次只有一個線程訪問一個對象、數(shù)據(jù)結(jié)構(gòu)或一段代碼。QMutex通常在較為簡單的代碼中使用,如果代碼復(fù)雜最好使用【QMutexLocker+互斥鎖】進(jìn)行多線程同步,這樣可以很容易確保鎖定和解鎖操作執(zhí)行一致。
? - 在復(fù)雜函數(shù)和語句或異常處理代碼中l(wèi)ock和unlock QMutex很容易出錯,而且很難調(diào)試。在這種情況下,可以使用QMutexLocker替代。
- ? QMutexLocker在一個需要鎖定QMutex的函數(shù)中創(chuàng)建。當(dāng)創(chuàng)建QMutexLocker時,互斥鎖被鎖定(后面可以使用unlock()和relock()對互斥鎖進(jìn)行解鎖和重新鎖定)。如果互斥鎖鎖定了,互斥對象將在QMutexLocker銷毀時被解鎖。
- 即QMutexLocker創(chuàng)建時鎖定,銷毀時解鎖,所以一般為局部變量
QMutex
預(yù)期:兩個線程使用一把鎖,操作一個數(shù)據(jù),數(shù)據(jù)會被兩個線程依次打印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; // 多個線程使用一把鎖
static int num; // 多個線程訪問一個數(shù)據(jù)
};
#endif // MYTHREAD_HMyThread.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);// 線程睡眠兩秒
}
}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_HmainWindow.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;
}運行結(jié)果:

QMutexLocker
預(yù)期:兩個線程使用一把鎖,操作一個數(shù)據(jù),數(shù)據(jù)會被兩個線程依次打印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)建的時候加鎖,當(dāng)QMutexLocker局部銷毀的時候解鎖
{
QMutexLocker lock(&this->mutex);
qDebug() << "Current Thread: " << this
<< ", Value: " << this->num++;
}
QThread::sleep(1);// 線程睡眠兩秒
}
}運行結(jié)果:

到此這篇關(guān)于Qt中互斥鎖QMutex和QMutexLocker的使用的文章就介紹到這了,更多相關(guān)Qt 互斥鎖 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++中拷貝構(gòu)造函數(shù)的應(yīng)用詳解
這篇文章主要介紹了C++中拷貝構(gòu)造函數(shù)的應(yīng)用,需要的朋友可以參考下2014-07-07
C++之實現(xiàn)快速清空vector以及釋放vector內(nèi)存
這篇文章主要介紹了C++之實現(xiàn)快速清空vector以及釋放vector內(nèi)存方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
C語言實現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出
今天小編就為大家分享一篇關(guān)于C語言實現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-12-12

