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



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)



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

  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这种方法慎用,因为如果信号发送者和接收者在同一个线程,将会导致死锁

Qt源码阅读(二) moveToThread的更多相关文章

  1. Qt源码阅读(四) 事件循环

    事件系统 文章为本人理解,如有理解不到位之处,烦请各位指正. @ 目录 事件系统 什么是事件循环? 事件是如何产生的? sendEvent postEvent 事件是如何处理的? 事件循环是怎么遍历的 ...

  2. xxl-job源码阅读二(服务端)

    1.源码入口 xxl-job-admin是一个简单的springboot工程,简单翻看源码,可以很快发现XxlJobAdminConfig入口. @Override public void after ...

  3. Qt源码阅读(三) 对象树管理

    对象树管理 个人经验总结,如有错误或遗漏,欢迎各位大佬指正 @ 目录 对象树管理 设置父对象的作用 设置父对象(setParent) 完整源码 片段分析 对象的删除 夹带私货时间 设置父对象的作用 众 ...

  4. Spring 源码阅读 二

    程序入口: 接着上一篇博客中看完了在AnnotationConfigApplicationContext的构造函数中的register(annotatedClasses);将我们传递进来的主配置类添加 ...

  5. SparkConf加载与SparkContext创建(源码阅读二)

    紧接着昨天,我们继续开搞了啊.. 1.下面,开始创建BroadcastManager,就是传说中的广播变量管理器.BroadcastManager用于将配置信息和序列化后的RDD.Job以及Shuff ...

  6. JDK源码阅读(二) AbstractList

    package java.util; public abstract class AbstractList<E> extends AbstractCollection<E> i ...

  7. QT源码解析(一) QT创建窗口程序、消息循环和WinMain函数

    QT源码解析(一) QT创建窗口程序.消息循环和WinMain函数 分类: QT2009-10-28 13:33 17695人阅读 评论(13) 收藏 举报 qtapplicationwindowse ...

  8. Struts2源码阅读(一)_Struts2框架流程概述

    1. Struts2架构图  当外部的httpservletrequest到来时 ,初始到了servlet容器(所以虽然Servlet和Action是解耦合的,但是Action依旧能够通过httpse ...

  9. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

  10. 【原】AFNetworking源码阅读(二)

    [原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...

随机推荐

  1. docker 1.13版本修改默认存储路径及添加私有registry

    1.服务器安装dockeryum install docker -y12.修改配置文件方法1: 编辑 `/etc/sysconfig/docker`文件 修改`OPTION`选项 添加 --insec ...

  2. server.error.include-message

    使用的thymeleaf模板引擎,默认前端无法获取message和exception 想要在前端获取到message和exception,配置一下配置 server.error.include-exc ...

  3. 加入security+jwt安全策略

    Pom中引入 <!-- security --> <dependency> <groupId>org.springframework.boot</groupI ...

  4. Qt编写物联网管理平台40-类型种类

    一.前言 为了增强本系统的拓展性,做成通用的物联网管理平台,特意将控制器主设备类型.探测器子设备类型.对应种类符号等信息,全部做成表格可自定义添加和修改,这样在控制器信息表和探测器信息表管理的时候,可 ...

  5. Qt编写安防视频监控系统54-轮询配置

    一.前言 视频监控系统中少不了用到视频轮询,按照设计的基本原则,先满足基本的用户需求,稳定跑起来,再去折腾更复杂的应用场景,于是本系统也做了个基本的视频轮询功能,可以设置轮询方案,给某个轮询方案设置轮 ...

  6. 阿里云maven仓库地址的配置

    两种配置方式: 1. maven 配置文件配置settings.xml中设置mirror节点 <mirror> <id>nexus-aliyun</id> < ...

  7. IM消息ID技术专题(七):深度解密vivo的自研分布式ID服务(鲁班)

    本文由vivo互联网技术An Peng分享,本文收录时有内容修订和重新排版. 1.引言 本文通过对分布式ID的3种应用场景.实现难点以及9种分布式ID的实现方式进行介绍,并对结合vivo业务场景特性下 ...

  8. 视频分析框架VideoPipe完整介绍

    (2024年4月编写) github地址 https://github.com/sherlockchou86/video_pipe_c 作者微信 zhzhi78(备注 videopipe),拉群交流( ...

  9. 压力测试-jmeter-copy

    1. 场景描述 新申请的服务器,要压测下python算法程序最多能执行多少条数据,有几年没用压力测试工具-jmeter了,重新下载了最新版本,记录下,也希望能帮到准备使用jmeter做压测的朋友. 2 ...

  10. dart中类详细讲解

    dart是一门面向对象的语言 dart是一门实用类和单继承的面向对象的语言 在dart中所有的对象都是类的实例. 所有的类都是Object的子类 类都是有属性和方法组成的 定义一个类 在dart中,我 ...