Qt中線程常用通信方式介紹
項目場景
Qt中,線程通信無處不在,最核心的特性信號槽就是一種線程間通信,安全可靠易用。除此之外,還有別的幾種常用的方式:
QMutex
互斥鎖,可以保護共享的數(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
使用事件隊列傳遞和處理,實現(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中線程常用通信方式介紹的詳細內(nèi)容,更多關(guān)于Qt線程通信的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++如何將一個vector內(nèi)容賦值給另一個vector,及swap與assign區(qū)別
在本文中,我們將主要介紹5種將一個vector內(nèi)容賦值給另一個vector的方式,順便討論下swap與assign的區(qū)別,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08
QT線程池的使用(QThreadPool類和QRunnable類)
本文主要介紹了QT線程池的使用(QThreadPool類和QRunnable類),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-04-04
C語言實現(xiàn)打印九九乘法表的四種方式小結(jié)
這篇文章主要為大家介紹了C語言實現(xiàn)打印九九乘法表的四種方式,文中的示例代碼講解詳細,簡潔易懂,感興趣的小伙伴可以跟隨小編一起學習一下2023-07-07
C++實現(xiàn)LeetCode(163.缺失區(qū)間)
這篇文章主要介紹了C++實現(xiàn)LeetCode(163.缺失區(qū)間),本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07

