Qt 线程常用通信方式

项目场景:

Qt中,线程通信无处不在,最核心的特性信号槽就是一种线程间通信,安全可靠易用。除此之外,还有别的几种常用的方式:


QMutex

互斥锁,可以保护共享的数据访问,例如对共享数据globalCounter得读写,可以保证数据的唯一和安全。

#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搭配使用,本质是等待某一线程释放mutex后,别的线程才可以使用,添加了事件执行条件,可以避免同一时间多个线程同时访问同一变量。

#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:

信号量可以控制访问特定资源的线程数量。

#include <QCoreApplication>
#include <QThread>
#include <QSemaphore>
#include <QDebug>

QSemaphore semaphore(3); // 允许同时访问资源的数量

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:

使用事件队列传递和处理,实现线程间的通信。

#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();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

雲烟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值