前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >QThread 的使用「建议收藏」

QThread 的使用「建议收藏」

作者头像
全栈程序员站长
发布2022-09-01 13:17:00
9930
发布2022-09-01 13:17:00
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

文章目录

1. 引言

你会用QThread吗?有几种使用方式?这几种使用方式都在何种场景下使用?有什么需要注意的地方吗?

2. QThread 文档

上来先看 Qt 帮助文档。QThread Class 文档,详细描述的第一句话:

代码语言:javascript
复制
The QThread class provides a platform-independent way to manage threads

注意看倒数第二个单词,QThread 不等于线程,QThread 是负责管理线程的。 接下来看文档,我们清楚的知道QThread的两种使用方式。

代码语言:javascript
复制
方式一:子类化QThread,并重新实现 run() 函数
方式二:定义工作对象继承自 QObject,然后把这个工作对象move到QThread的一个对象中。

3. QThread::run 和 QObject::connect

先看下帮助文档上怎么描述这个 QThread::run 函数的: The starting point for the thread. After calling start(), the newly created thread calls this function. The default implementation simply calls exec(). You can reimplement this function to facilitate advanced thread management. Returning from this method will end the execution of the thread. 这段英文描述的很清楚。好,下面看例子:

先看文档上的例子:

代码语言:javascript
复制
class Worker : public QObject
{
    Q_OBJECT
public:
    Worker(){}
public slots:
    void emitsig()
    {
        emit sig();
    }
signals:
    void sig();
};

class Thread : public QThread
{
    Q_OBJECT
public:
	Thread(QObject* parent=0):QThread(parent)
	{
	}

	void fun()
	{
		qDebug() << "Thread::fun threadID: " << QThread::currentThreadId();
	}
public slots:
	void slotFun()
	{
		qDebug() << "Thread::slotFun threadID: " << QThread::currentThreadId();
	}
signals:
    void sig();
protected:
	void run()
	{
		QThread::sleep(5);
        qDebug() <<"Thread::run threadID: " << currentThreadId();
        fun();
        slotFun();
        exec();
		qDebug() << "Thread:: exit";
	}
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Thread thread;
    Worker work;
    QObject::connect(&work, SIGNAL(sig()), &thread, SLOT(slotFun()));
    thread.start();

    qDebug() << "Main threadID: " << QThread::currentThreadId();
    thread.fun();
    thread.slotFun();

	qDebug() << "before signal. threadID: " <<  QThread::currentThreadId();
    work.emitsig();
	qDebug() << "after signal. threadID: " <<  QThread::currentThreadId();

    return a.exec();
}

此处不考虑线程资源回收问题。请问打印输出的结果是什么?线程ID是否一样呢?

公布结果之前,先看下关于 QObject::connect 函数相关说明:

涉及信号槽,我们就躲不过 connect 函数,只是这个函数大家太熟悉。我不好意思再用一堆废话来描述它,但不说又不行,那么折中一下,只看它的最后一个参数吧(为了简单起见,只看它最常用的3个值):

  • 自动连接(Auto Connection)
    • 这是默认设置
    • 如果信号在接收者所依附的线程内发射,则等同于直接连接
    • 如果发射信号的线程和接受者所依附的线程不同,则等同于队列连接
    • 也就是这说,只存在下面两种情况(直接连接和队列连接)
  • 直接连接(Direct Connection)
    • 当信号发射时,槽函数将直接被调用。
    • 无论槽函数所属对象在哪个线程,槽函数都在发射信号的线程内执行。
  • 队列连接(Queued Connection)
    • 当控制权回到接受者所依附线程的事件循环时,槽函数被调用。
    • 槽函数在接收者所依附线程执行。

看了这些说明之后,你对刚才脑子里的结果是否确认呢? 下面公布结果:

代码语言:javascript
复制
Main threadID:  0x3318
Thread::fun threadID:  0x3318
Thread::slotFun threadID:  0x3318
before signal. threadID:  0x3318
Thread::slotFun threadID:  0x3318
after signal. threadID:  0x3318
Thread::run threadID:  0x3e30
Thread::fun threadID:  0x3e30
Thread::slotFun threadID:  0x3e30

为什么会有这样的结果呢?

因为:

  • QThread 是用来管理线程的,它所依附的线程和它管理的线程并不是同一个东西
  • QThread 所依附的线程,就是执行 QThread t(0) 或 QThread * t=new QThread(0) 的线程。也就是咱们这儿的主线程
  • QThread 管理的线程,就是 run 启动的线程。也就是次线程
    • 因为 QThread 的对象依附在主线程中,所以他的 slot 函数会在主线程中执行,而不是次线程。除非:QThread 对象依附到次线程中(通过movetoThread)
    • slot 和信号是直接连接,且信号在次线程中发射

槽函数呢,其实就可以当成普通函数来使用。相信大家看完输出结果和原因分析之后,应该能理解为什么是这样的输出了。

下面再看一个例子:

代码语言:javascript
复制
class Thread : public QThread
{
    Q_OBJECT
private slots:
    void onTimeout()
    {
        qDebug() << "Thread::onTimeout get called from? : " << QThread::currentThreadId();
    }

private:
    void run()
    {
        qDebug() << "From worker thread: " << currentThreadId();
        QTimer timer;
        connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeout()));
        timer.start(1000);

        exec();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

	qDebug() << "Main threadID: " << QThread::currentThreadId();
    Thread thread;
    thread.start();

    return a.exec();
}

看下打印结果:

代码语言:javascript
复制
Main threadID:  0x2e78
From worker thread:  0x2998
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
Thread::onTimeout get called from? :  0x2e78
...

是不是跟我们心里预期的不一样?我想让一些耗时操作放到 onTimeOut 里面去处理,并且由子线程来处理啊,怎么执行在主线程里?

因为 this 指的是Thread,因为 connect 最后一个参数是自动连接,信号发射者和接收者所依附的线程不同,所以是队列连接。那要怎么改呢? connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeout()), Qt::DirectConnection); 但其实这也是不好的实现方式,因为 onTimeout 是 Thread 对象的成员函数,但却被它所创建的子线程调用。所以我们引出第二种使用方式。

如果把例子中 Thread 的 run 函数里面的 exec() 去掉会怎样呢?

4. QObject::moveToThread()

再来对上一个例子修改一下:

代码语言:javascript
复制
class Worker : public QObject
{
    Q_OBJECT
public slots:
    void onTimeout()
    {
        qDebug() << "Worker::onTimeout get called from?: " << QThread::currentThreadId();
    }
};

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    qDebug() << "From main thread: " << QThread::currentThreadId();

    QThread thread;
    QTimer timer;
    Worker worker;

    QObject::connect(&timer, SIGNAL(timeout()), &worker, SLOT(onTimeout()));
    timer.start(1000);

    worker.moveToThread(&thread);
    thread.start();

    return a.exec();
}

这样就是我们想要的方式了。来看下打印结果:

代码语言:javascript
复制
From main thread:  0x1238
Worker::onTimeout get called from?:  0x2bb0
Worker::onTimeout get called from?:  0x2bb0
Worker::onTimeout get called from?:  0x2bb0
Worker::onTimeout get called from?:  0x2bb0
Worker::onTimeout get called from?:  0x2bb0

5. 使用场景

既然两种方式都可以使用,哪是不是随便使用其中一种方式就可以了呢?我们还是要具体问题,具体分析。哪个合适使用哪个。就像之前说过的,C 语言也能实现面向对象,但是 C++ 实现起来更快更方便而已。

对于子类化 Thread 的方式

这种方式适用于一些任务场景:

  • 很多经典线程问题(生产者,消费者等)
  • 独立不依赖的一些工作任务

这种方式有一些特点:

  • 不需要事件循环,一次性的执行
  • 不需要被调用槽函数
  • 可以自己定义 run() 函数的实现

这种方式有一些陷阱:

  • 提供槽函数,子类化对象是属于主线程的,又没有事件循环,所以槽函数会被主线程执行
  • 调用 moveToThread(this)

对于 worker move to thread 的方式

这种方式适用于一些任务场景:

  • 内部独立型的一些任务
  • “管理”任务

这种方式有一些特点:

  • 事件驱动型
  • 需要和外部进行通信
  • 没有一个单一的入口点

这种方式有一些陷阱:

  • 这个任务到底需不需要事件驱动?
  • 处理太耗时,而不把权利交给事件循环
  • 假的事件循环,入口点一直不退出,不能把权利交给事件循环

QThread资料

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/140235.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 1. 引言
  • 2. QThread 文档
  • 3. QThread::run 和 QObject::connect
  • 4. QObject::moveToThread()
  • 5. 使用场景
    • 对于子类化 Thread 的方式
      • 对于 worker move to thread 的方式
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档