亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Qt中互斥鎖QMutex和QMutexLocker的使用

 更新時(shí)間:2023年05月24日 08:36:07   作者:小瑞的學(xué)習(xí)筆記  
本文主要介紹了Qt中互斥鎖QMutex和QMutexLocker的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

QMutex和QMutexLocker

類 QMutex 的主要函數(shù)有:

  • lock (); 加鎖,如果該互斥鎖被占用,該函數(shù)阻塞,直到互斥鎖被釋放。
  • unlock ();
  • 解鎖bool tryLock (int timeout = 0);

表示嘗試去加鎖,timeout 為超時(shí)時(shí)間。如果互斥鎖為可用狀態(tài),該函數(shù)會占用該互斥鎖,并返回 true ,否則返回 false 。如果互斥鎖被另一個(gè)線程占用,該函數(shù)會等待 timeout 毫秒直到互斥鎖為可用狀態(tài)。

QMutexLocker 類的主要作用是用來管理 QMutex使用 QMutexLocker 的好處是,可以防止線程死鎖。該對象在構(gòu)造的時(shí)候加鎖,析構(gòu)的時(shí)候解鎖。

使用場景

QMutex目的是保護(hù)一次只有一個(gè)線程訪問一個(gè)對象、數(shù)據(jù)結(jié)構(gòu)或一段代碼。QMutex通常在較為簡單的代碼中使用,如果代碼復(fù)雜最好使用【QMutexLocker+互斥鎖】進(jìn)行多線程同步,這樣可以很容易確保鎖定和解鎖操作執(zhí)行一致。

? - 在復(fù)雜函數(shù)和語句或異常處理代碼中l(wèi)ock和unlock QMutex很容易出錯(cuò),而且很難調(diào)試。在這種情況下,可以使用QMutexLocker替代。

  • ? QMutexLocker在一個(gè)需要鎖定QMutex的函數(shù)中創(chuàng)建。當(dāng)創(chuàng)建QMutexLocker時(shí),互斥鎖被鎖定(后面可以使用unlock()和relock()對互斥鎖進(jìn)行解鎖和重新鎖定)。如果互斥鎖鎖定了,互斥對象將在QMutexLocker銷毀時(shí)被解鎖。
  • 即QMutexLocker創(chuàng)建時(shí)鎖定,銷毀時(shí)解鎖,所以一般為局部變量

QMutex

預(yù)期:兩個(gè)線程使用一把鎖,操作一個(gè)數(shù)據(jù),數(shù)據(jù)會被兩個(gè)線程依次打印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è)線程使用一把鎖
    static int num;       // 多個(gè)線程訪問一個(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);// 線程睡眠兩秒
    }
}

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è)線程使用一把鎖,操作一個(gè)數(shù)據(jù),數(shù)據(jù)會被兩個(gè)線程依次打印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局部銷毀的時(shí)候解鎖
        {
            QMutexLocker lock(&this->mutex);
            qDebug() << "Current Thread: " << this
                      << ", Value: " << this->num++;
        }
        QThread::sleep(1);// 線程睡眠兩秒
    }
}

運(yùn)行結(jié)果:

到此這篇關(guān)于Qt中互斥鎖QMutex和QMutexLocker的使用的文章就介紹到這了,更多相關(guān)Qt 互斥鎖 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論