最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Qt中線程常用通信方式介紹

 更新時間:2025年01月15日 08:35:16   作者:雲(yún)煙  
Qt中,線程通信無處不在,最核心的特性信號槽就是一種線程間通信,這篇文章主要為大家介紹了幾種常用的方式,需要的小伙伴可以參考一下

項(xiàng)目場景

Qt中,線程通信無處不在,最核心的特性信號槽就是一種線程間通信,安全可靠易用。除此之外,還有別的幾種常用的方式:

QMutex

互斥鎖,可以保護(hù)共享的數(shù)據(jù)訪問,例如對共享數(shù)據(jù)globalCounter得讀寫,可以保證數(shù)據(jù)的唯一和安全。

#include <QCoreApplication>
#include <QThread>
#include <QMutex>
#include <QDebug>
 
QMutex mutex;
int globalCounter = 0;
 
class Worker : public QThread {
protected:
    void run() override {
        for (int i = 0; i < 1000; ++i) {
            mutex.lock();
            ++globalCounter;
            mutex.unlock();
        }
    }
};
 
int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
 
    Worker worker1, worker2;
    worker1.start();
    worker2.start();
 
    worker1.wait();
    worker2.wait();
 
    qDebug() << "Global counter:" << globalCounter;
 
    return 0;
}

QWaitCondition

條件等待通常與QMutex搭配使用,本質(zhì)是等待某一線程釋放mutex后,別的線程才可以使用,添加了事件執(zhí)行條件,可以避免同一時間多個線程同時訪問同一變量。

#include <QCoreApplication>
#include <QThread>
#include <QMutex>
#include <QWaitCondition>
#include <QDebug>
 
QMutex mutex;
QWaitCondition condition;
bool ready = false;
 
class Producer : public QThread {
protected:
    void run() override {
        mutex.lock();
        qDebug() << "Producer is producing.";
        ready = true;
        condition.wakeOne();
        mutex.unlock();
    }
};
 
class Consumer : public QThread {
protected:
    void run() override {
        mutex.lock();
        if (!ready) {
            condition.wait(&mutex);
        }
        qDebug() << "Consumer is consuming.";
        mutex.unlock();
    }
};
 
int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
 
    Producer producer;
    Consumer consumer;
 
    consumer.start();
    producer.start();
 
    consumer.wait();
    producer.wait();
 
    return 0;
}

QSemaphore

信號量可以控制訪問特定資源的線程數(shù)量。

#include <QCoreApplication>
#include <QThread>
#include <QSemaphore>
#include <QDebug>
 
QSemaphore semaphore(3); // 允許同時訪問資源的數(shù)量
 
class Worker : public QThread {
protected:
    void run() override {
        semaphore.acquire();
        qDebug() << "Worker is accessing resource in thread:" << QThread::currentThread();
        QThread::sleep(1); // 模擬工作
        semaphore.release();
    }
};
 
int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
 
    Worker workers[10];
    for (auto &worker : workers) {
        worker.start();
    }
 
    for (auto &worker : workers) {
        worker.wait();
    }
 
    return 0;
}

QEvent

使用事件隊(duì)列傳遞和處理,實(shí)現(xiàn)線程間的通信。

#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include <QEvent>
#include <QApplication>
 
class CustomEvent : public QEvent {
public:
    static const QEvent::Type EventType = static_cast<QEvent::Type>(QEvent::User + 1);
    CustomEvent(const QString &message) : QEvent(EventType), message(message) {}
    QString message;
};
 
class EventReceiver : public QObject {
protected:
    bool event(QEvent *event) override {
        if (event->type() == CustomEvent::EventType) {
            CustomEvent *customEvent = static_cast<CustomEvent *>(event);
            qDebug() << "Received custom event with message:" << customEvent->message;
            return true;
        }
        return QObject::event(event);
    }
};
 
class EventSender : public QThread {
protected:
    void run() override {
        QThread::sleep(1);
        qApp->postEvent(receiver, new CustomEvent("Hello from another thread!"));
    }
public:
    EventReceiver *receiver;
};
 
int main(int argc, char *argv[]) {
    QCoreApplication app(argc, argv);
 
    EventReceiver receiver;
    EventSender sender;
    sender.receiver = &receiver;
 
    sender.start();
    sender.wait();
 
    return app.exec();
}

以上就是Qt中線程常用通信方式介紹的詳細(xì)內(nèi)容,更多關(guān)于Qt線程通信的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • boost.asio框架系列之socket編程

    boost.asio框架系列之socket編程

    這篇文章介紹了boost.asio框架系列之socket編程,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-06-06
  • C++常用的#include頭文件總結(jié)

    C++常用的#include頭文件總結(jié)

    這篇文章主要介紹了C++常用的#include頭文件,對初學(xué)者理解C++程序設(shè)計大有好處的相關(guān)資料
    2014-07-07
  • C++語言中std::array的用法小結(jié)(神器用法)

    C++語言中std::array的用法小結(jié)(神器用法)

    這篇文章主要介紹了C++語言中std::array的用法小結(jié),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-11-11
  • 基于C++的數(shù)據(jù)庫連接池的實(shí)現(xiàn)示例

    基于C++的數(shù)據(jù)庫連接池的實(shí)現(xiàn)示例

    本文主要介紹了基于C++的數(shù)據(jù)庫連接池,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2025-11-11
  • linux中查詢dns示例

    linux中查詢dns示例

    這篇文章主要介紹了linux中查詢dns示例,需要的朋友可以參考下
    2014-04-04
  • 優(yōu)秀程序員必須知道的20個位運(yùn)算技巧

    優(yōu)秀程序員必須知道的20個位運(yùn)算技巧

    掌握簡單的位運(yùn)算技巧還是必要的,所以今天寫這篇文章把我積累的一些位運(yùn)算技巧分享給大家,這些技巧不會是如求“1的數(shù)目”的技巧,是最基本的一行位運(yùn)算技巧
    2013-09-09
  • C語言實(shí)現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出

    C語言實(shí)現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出

    今天小編就為大家分享一篇關(guān)于C語言實(shí)現(xiàn)模擬USB對8bit數(shù)據(jù)的NRZI編碼輸出,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2018-12-12
  • C/C++實(shí)現(xiàn)MD5校驗(yàn)學(xué)習(xí)

    C/C++實(shí)現(xiàn)MD5校驗(yàn)學(xué)習(xí)

    MD5全程Message-Digest?Algorithm?5,即消息摘要算法第五版,是屬于hash算法的一種,本文主要介紹了如何在C++中實(shí)現(xiàn)MD5校驗(yàn),需要的可以了解下
    2024-03-03
  • C++實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)化為后綴表達(dá)式詳解

    C++實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)化為后綴表達(dá)式詳解

    這篇文章主要為大家詳細(xì)介紹了如何利用C++解決實(shí)現(xiàn)中綴表達(dá)式轉(zhuǎn)換為后綴表達(dá)式的問題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 基于MFC實(shí)現(xiàn)類的序列化詳解

    基于MFC實(shí)現(xiàn)類的序列化詳解

    序列化是將程序中的對象以一種二進(jìn)制格式存儲到存儲設(shè)備中(例如文本/數(shù)據(jù)庫等),以實(shí)現(xiàn)“永生”或隨意“流動”。本文將為大家詳細(xì)講講如何基于MFC實(shí)現(xiàn)類的序列化,需要的可以參考一下
    2022-07-07

最新評論

都江堰市| 吴堡县| 磐安县| 东兴市| 邻水| 淳安县| 吉木乃县| 故城县| 新余市| 韶关市| 繁峙县| 龙岩市| 英吉沙县| 吉首市| 姜堰市| 南汇区| 盐津县| 叙永县| 中超| 高雄县| 九寨沟县| 农安县| 佳木斯市| 安陆市| 阿克陶县| 安福县| 天津市| 涿鹿县| 开阳县| 财经| 会理县| 新龙县| 繁昌县| 潮州市| 洪雅县| 如皋市| 绿春县| 宜城市| 得荣县| 芒康县| 永顺县|