首页 > 编程语言 >Qt源码阅读(二) moveToThread

Qt源码阅读(二) moveToThread

时间:2023-03-26 16:13:23浏览次数:41  
标签:moveToThread Qt thread 对象 QObject 源码 线程

Qt 源码分析之moveToThread

这一次,我们来看Qt中关于将一个QObject对象移动至一个线程的函数moveToThread

目录

Qt使用线程的基本方法

首先,我们简单的介绍一下在Qt中使用多线程的几种方法:

  1. 重写QThreadrun函数,将要在多线程执行的任务放到run函数里
/*mythread.h*/
#pragma once

#include <QThread>

class MyThread  : public QThread
{
    Q_OBJECT

public:
    explicit MyThread(QObject* parent = nullptr);
    ~MyThread();

protected:
    void run() override;
};

/*mythread.cpp*/
#include "mythread.h"
#include <QDebug>

MyThread::MyThread(QObject* parent)
    : QThread(parent)
{}

MyThread::~MyThread()
{}

void MyThread::run()
{
    /*
        在这个函数里执行耗时操作
    */
   for (auto a = 0; a < 10; a++) {
       qDebug() << u8"线程";
       QThread::sleep(1);
   }
}

/*调用函数*/
auto m_thread = new MyThread();
// 调用start之后,就会去执行run里内容了
m_thread->start();

但是这种方法,不被Qt官方所推荐,Qt官方所推荐的是将对象移动至线程的方法moveToThread

  1. 创建一个QThread对象,将对象移动至一个线程中,用信号槽的方式来触发该对象的槽函数,此时槽函数是在线程中执行的
/*mytask.h*/
#pragma once

#include <QObject>

class MyTask  : public QObject
{
    Q_OBJECT

public:
    MyTask(QObject *parent = nullptr);
    ~MyTask();

public slots:
    void slotMyTask();
};

/*mytask.cpp*/
#include "mytask.h"
#include <QThread>
#include <QDebug>

MyTask::MyTask(QObject *parent)
    : QObject(parent)
{}

MyTask::~MyTask()
{}

void MyTask::slotMyTask()
{
    /* 在这里执行耗时操作 */
    for (auto a = 0; a < 10; a++) {
        qDebug() << u8"当前线程: " << QThread::currentThread();
        qDebug() << u8"线程";
        QThread::sleep(1);
    }
}


/*使用方法*/
// 1. 创建任务对象以及线程对象
auto m_task = new MyTask();
auto* m_thread = new QThread();

// 2. 将任务对象移动至线程
m_task->moveToThread(m_thread);

// 3. 将信号与任务类的槽连接起来
connect(m_thread, &QThread::started, m_task, &MyTask::slotMyTask);

//  4. 开启线程
m_thread->start();

image.png
Note:
这里有一个坑,那就是如果一个QObject对象是有父对象的,那么该对象,就不能被移动至线程。测试代码如下:

// 1. 创建一个有父对象的任务对象以及线程对象
auto m_task = new MyTask(this);
auto* m_thread = new QThread();

// 2. 将任务对象移动至线程
m_task->moveToThread(m_thread);

// 3. 将信号与任务类的槽连接起来
connect(m_thread, &QThread::started, m_task, &MyTask::slotMyTask);

//  4. 开启线程
m_thread->start();

此时,我们看到控制台会输出:

Cannot move objects with a parent (无法移动一个有父对象的object)

image.png
并且,我们能看到槽函数里打印的线程为主线程

  1. 使用Qt的QtConcurrent,缺点之一是没有办法手动退出
// 使用这个,需要在头文件里引入
#include <QtConcurrent/QtConcurrent>

// 定义一个任务函数
int MainWindow::taskTest(int a)
{
    for (auto i = 1; i < 10; i++) {
        qDebug() << "a: " << a;
        QThread::sleep(1);
    }

    return 0;
}

/* 使用方法 */
// 在函数后面跟上你要设置给函数的参数
QtConcurrent::run(this, &MainWindow::taskTest, 10);

注意:在Qt里,子线程不能进行任何的ui更新操作,ui的更新操作全部只能在主线程

源码分析

然后,我们浅浅的分析一下,QObject中的moveToThread,主要分为三个部分

  1. 对一些基本条件的判断:
    • 移动的对象是否已经在目标线程

    • 移动的对象是否有父对象(这就是我们上面说到的坑)

    • 不能将一个窗口对象移动至其他线程,因为Qt要求所有UI操作都必须在主线程中执行,线程中如果想要更新UI,需要用信号槽来通知界面进行更改。

// 当前对象已经在目标线程了
    if (d->threadData.loadRelaxed()->thread.loadAcquire() == targetThread) {
        // object is already in this thread
        return;
    }

	// 不能移动一个有父对象的对象
    if (d->parent != nullptr) {
        qWarning("QObject::moveToThread: Cannot move objects with a parent");
        return;
    }
	// 窗口部件不能移动到一个新的线程,在Qt里GUI操作只能在主线程
    if (d->isWidget) {
        qWarning("QObject::moveToThread: Widgets cannot be moved to a new thread");
        return;
    }
  1. 对要移动的对象当前所属线程的一些判断:
    • 如果要移动的对象没有线程依附性,那么可以移动至目标线程

    • 如果移动操作所在线程与移动对象所在线程不一致,那么不允许去移动

QThreadData *currentData = QThreadData::current();
QThreadData *targetData = targetThread ? QThreadData::get2(targetThread) : nullptr;
QThreadData *thisThreadData = d->threadData.loadRelaxed();
if (!thisThreadData->thread.loadAcquire() && currentData == targetData) {
    // 如果一个对象没有线程依附性,允许移动一个对象到一个线程
    // one exception to the rule: we allow moving objects with no thread affinity to the current thread
    currentData = d->threadData;
} else if (thisThreadData != currentData) {
    // 不能在不是对象的线程里,去移动该对象至另外一个对象
    qWarning("QObject::moveToThread: Current thread (%p) is not the object's thread (%p).\n"
             "Cannot move to target thread (%p)\n",
             currentData->thread.loadRelaxed(), thisThreadData->thread.loadRelaxed(), targetData ? targetData->thread.loadRelaxed() : nullptr);

#ifdef Q_OS_MAC
    qWarning("You might be loading two sets of Qt binaries into the same process. "
             "Check that all plugins are compiled against the right Qt binaries. Export "
             "DYLD_PRINT_LIBRARIES=1 and check that only one set of binaries are being loaded.");
#endif

    return;
}
  1. 正式的移动操作

    // prepare to move
    d->moveToThread_helper();

    if (!targetData)
        targetData = new QThreadData(0);

    // make sure nobody adds/removes connections to this object while we're moving it
    QMutexLocker l(signalSlotLock(this));

    QOrderedMutexLocker locker(&currentData->postEventList.mutex,
                               &targetData->postEventList.mutex);

    // keep currentData alive (since we've got it locked)
    currentData->ref();

    // move the object
    d_func()->setThreadData_helper(currentData, targetData);

    locker.unlock();

    // now currentData can commit suicide if it wants to
    currentData->deref();

一些线程和信号槽使用的心得

到了夹带私活时间,下面是一些多线程使用信号槽的一点小心得总结

  1. 不能在子线程去更新UI界面,只能在主线程进行更新
  2. 可以通过信号槽连接,在子线程通知主线程去更新UI
  3. 跨线程使用信号槽,建议用QueuedConnection,因为这种连接方式,Qt会把信号丢到事件循环里去,这样槽函数会在接收者所在的线程执行。而DirectConnection这种连接方式,因为是直接回调槽函数,槽会在信号发出的线程进行调用。具体可看上篇关于信号与槽源码分析。
  4. 但是使用QueuedConnection这种连接方式,信号的参数如果是自己定义的类型,一定要记得使用qRegisterMetaType来进行注册,或者使用Q_DECLARE_METATYPE来进行注册。否则,槽函数将不会触发。
  5. BlockQueuedConnection这种方法慎用,因为如果信号发送者和接收者在同一个线程,将会导致死锁

标签:moveToThread,Qt,thread,对象,QObject,源码,线程
From: https://www.cnblogs.com/codegb/p/17258617.html

相关文章

  • Mybatis源码(十一):Mybatis与Spring的整合
    一、搭建mybtais-spring运行环境1、创建数据表并初始化CREATETABLE`user`(`id`int(8)NOTNULLAUTO_INCREMENTCOMMENT'主键',`name`varchar(32)CHARACTE......
  • spring源码环境搭建
    spring源码环境搭建组件版本jdk1.8.0_192spring-framework5.3.xgradle7.5.1idea2022.3.3aspectJ1.9可根据spring-framwork项目说明灵活选择......
  • MapReduce Shuffle源码解读
    MapReduceShuffle源码解读相信很多小伙伴都背过shuffle的八股文,但一直不是很理解shuffle的过程,这次我通过源码来解读下shuffle过程,加深对shuffle的理解,但是我自己还是个......
  • 跨境外贸可翻译客服系统源码实现,访客消息可翻译为中文,客服消息可以转为外语发送
    要实现跨境外贸的可翻译客服系统,我们需要一个能够将多种语言互相转换的翻译API。常用的翻译API包括GoogleTranslateAPI、MicrosoftTranslatorAPI等。在本示例中,我......
  • Mybatis源码(十):Mybatis插件机制
    1、Mybatis插件支持拦截的对象MyBatis允许使用插件来拦截的方法调用,可在映射语句执行流程中进行拦截调用。Mybatis插件支持拦截的对象:1、Executor:执行器Execu......
  • Qt音视频开发29-ffmpeg中x264/x265编码库支持
    一、前言有了解码当然对应又有编码,编码是信息从一种形式或格式转换为另一种形式的过程也称为计算机编程语言的代码简称编码。用预先规定的方法将文字、数字或其它对象编成......
  • 【Visual Leak Detector】QT 中 VLD 输出解析(一)
    说明使用VLD内存泄漏检测工具辅助开发时整理的学习笔记。目录说明1.使用方式2.无内存泄漏时的输出报告1.使用方式在QT中使用VLD的方法可以查看另外几篇博客:......
  • modbus CRC校验源码转载
     c#CRC校验 用于学习记录原文载自:https://www.cnblogs.com/ayxj/p/11481969.html用C#实现的几种常用数据校验方法整理(CRC校验;LRC校验;BCC校验;累加和校验)   ......
  • Qt源码阅读(一) 信号槽的连接与调用
    信号槽连接目录信号槽连接1.信号的连接2槽的调用信号槽的连接,其实内部本质还是一个回调函数,主要是维护了信号发送Object的元对象里一个连接的列表。调用connect函数时,......
  • 若依框架----源码分析(@RateLimiter)
    若依作为最近非常火的脚手架,分析它的源码,不仅可以更好的使用它,在出错时及时定位,也可以在需要个性化功能时轻车熟路的修改它以满足我们自己的需求,同时也可以学习人家解决问题......