QT multithreading inheritance QThread

1 Overview

In Qt, you can use QThread in two ways to create and control threads: inheriting the QThread class and using the QObject::moveToThread() method. The previous article introduced the method of QObject::moveToThread(), and this article introduces the method of inheriting the QThread class.

This is a more traditional way of using QThread, you can create a subclass inherited from the QThread class, and implement the run() method in the subclass, write the code executed by the thread in this method, and then call it in the main thread start() function to start the child thread.

2. Method description

The following is a sample code for creating a thread by inheriting from QThread:

class MyThread : public QThread
{
    
    
public:
    void run() override
    {
    
    
        // 线程执行的代码
        // ...
    }
};

// 创建线程对象
MyThread* thread = new MyThread();

// 启动线程
thread->start();

 
Please add a picture description

 

3. Code:

First write the MyThread class, which inherits from the QThread class, in which the signals and slots are customized, and the run function is rewritten. The header file is as follows:

//
// MyThread.h
//
#ifndef DEL2_MYTHREAD_H
#define DEL2_MYTHREAD_H

#include <QThread>
#include <QDebug>

class MyThread : public QThread
{
    
    
Q_OBJECT

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

signals:
    void myThreadSignal(const int);  // 自定义发送的信号

public slots:
    static void myThreadSlot(int);  // 自定义槽函数

protected:
    void run() override ;
};

#endif //DEL2_MYTHREAD_H

mythread.cpp:

#include "mythread.h"

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

//2. 新线程中的 0x4684
void MyThread::run()
{
    
    
    qDebug() << __FUNCTION__ << " Thread ID: " << QThread::currentThreadId() << "\n";

    emit myThreadSignal(100);  // 发送结束信号

    exec();
}

//3. 主线程中的  0x5120
void MyThread::myThreadSlot(const int val)
{
    
    
    qDebug() << __FUNCTION__ << " Thread ID: " << QThread::currentThreadId() <<  ", val: " << val <<"\n";
}

Implement the call of MyThread in the ThreadController class

#include "threadcontroller.h"

ThreadController::ThreadController(QObject *parent) : QObject(parent)
{
    
    
    _myThread = new MyThread ;

    connect(_myThread, &MyThread::myThreadSignal, this, &ThreadController::handleResults);

    // 该线程结束时销毁
    connect(_myThread, &QThread::finished, this, &QObject::deleteLater);

    connect(this, &ThreadController::operate, _myThread, &MyThread::myThreadSlot);

    // 启动该线程
    _myThread->start();

    // 当前线程挂起 3s
    QThread::sleep(3);

    //3. --- 先执行当前槽函数,再执行run中槽函数
    emit operate(99);
}

ThreadController::~ThreadController()
{
    
    
    _myThread->quit();
    _myThread->wait();
}

//4. 主线程中的  0x5120
void ThreadController::handleResults(const int result)
{
    
    
    qDebug() << __FUNCTION__ << " Thread ID:" << QThread::currentThreadId() << ", last result: " << result;
}

**threadcontroller.h: **

#ifndef THREADCONTROLLER_H
#define THREADCONTROLLER_H

#include <QObject>
#include "mythread.h"

class ThreadController : public QObject
{
    
    
Q_OBJECT

public:
    explicit ThreadController(QObject *parent = nullptr);

    ~ThreadController() override ;

public slots:
    static void handleResults(int result);  // 处理子线程执行的结果

signals:
    void operate(const int);  // 发送信号,触发线程

private:
    MyThread* _myThread ;

};

#endif // THREADCONTROLLER_H

Next is the main function. In the main function, we create a new Controller object and start executing:

#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include "threadcontroller.h"

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

    qDebug() << __FUNCTION__ << " Thread ID:" << QThread::currentThreadId() << '\n' ;

    ThreadController c ;

    return a.exec(); // 进入事件循环
}


4. Running results

insert image description here

 

Notice:

  1. Only the ones in run() are new threads, and the rest are the main threads!
  2. QThread::sleep(3); After using, the current main thread hangs for 3 seconds, while run() in the child thread continues to execute, and the slot function of the main thread will run after the sleep is over!

5 Conclusion

The method of subclassing QThread is to rewrite the run() function in QThread, and define the required work in the run() function. The result of this is that after our custom sub-thread calls the start() function, it starts to execute the run() function. If you define related slot functions in the custom thread class MyThread, these slot functions will not be executed by the subclassed QThread's own event loop, but by the owner of the sub-thread (usually the main thread) implement.

The thread design of qt does not hope to achieve multi-threading through inheritance, but hopes that the thread can also be used directly as an object, that is, the moveToThread method of QObject

This article is only a preliminary study, not focusing on the release and creation of threads, and the race problems caused by multi-threading~

Reference: Multithreading in QT - Inheriting QThread

Guess you like

Origin blog.csdn.net/qq_16504163/article/details/130462012