栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

笔记-Qt多线程

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

笔记-Qt多线程

笔记-Qt多线程 QThread-线程

实现方式1, 不推荐
继承QThread对象, 然后重新实现虚函数QThread::run

class WorkerThread : public QThread
{
    Q_OBJECT
    void run() override {
        QString result;
        
        emit resultReady(result);
    }
signals:
    void resultReady(const QString &s);
};

void MyObject::startWorkInAThread()
{
    WorkerThread *workerThread = new WorkerThread(this);
    connect(workerThread, &WorkerThread::resultReady, this, &MyObject::handleResults);
    connect(workerThread, &WorkerThread::finished, workerThread, &QObject::deleteLater);
    workerThread->start();
}

实现方式2, 推荐
对于继承Object的对象, 通过QObject::moveToThread(QThread *targetThread)改变对象和它的子对象的线程关联性, 再利用信号槽机制, 将定义在对象中成员函数放在targetThread中运行;
需要注意, 如果对象有父对象, 着无法移动成功.

class Worker : public QObject
{
    Q_OBJECT

public slots:
    void doWork(const QString ¶meter) {
        QString result;
        
        emit resultReady(result);
    }

signals:
    void resultReady(const QString &result);
};

class Controller : public QObject
{
    Q_OBJECT
    QThread workerThread;
public:
    Controller() {
        Worker *worker = new Worker;
        worker->moveToThread(&workerThread);
        connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
        connect(this, &Controller::operate, worker, &Worker::doWork);
        connect(worker, &Worker::resultReady, this, &Controller::handleResults);
        workerThread.start();
    }
    ~Controller() {
        workerThread.quit();
        workerThread.wait();
    }
public slots:
    void handleResults(const QString &);
signals:
    void operate(const QString &);
};
QThreadPool-线程池

qt中线程池需要注意添加到线程池的对象必须继承至QRunnable对象, 在实现QRunnable::run, 线程池会分配线程去执行QRunnable::run函数, 对于QRunnable对象的生命周期问题, 可以通过void QRunnable::setAutoDelete(bool autoDelete)设置autoDelete=true来规避;

class HelloWorldTask : public QRunnable
{
    void run()
    {
        qDebug() << "Hello world from thread" << QThread::currentThread();
    }
}

HelloWorldTask *hello = new HelloWorldTask();
// QThreadPool takes ownership and deletes 'hello' automatically
QThreadPool::globalInstance()->start(hello);
QtConcurrent-并发任务

QtConcurrent其实就是将函数添加到QThreadPool::globalInstance()中, 可以当作是QThreadPool+QRunable的简化操作吧

QFuture QtConcurrent::run(Function function, ...)
相当于: QtConcurrent::run(QThreadPool::globalInstance(), function, ...);
QFuture QtConcurrent::run(QThreadPool *pool, Function function, ...)

QtConcurrent::run([]() {
    qDebug() << "current thread id=" << QThread::currentThreadId() << " time=" 
                << QDateTime::currentDateTime().toString("HH:mm:ss:zzz");
    QThread::sleep(1);
});
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/738913.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号