Qt——线程的生命期问题

1.C++对象有生命周期,线程也有生命周期,QThread对象的生命周期与对应的线程生命周期是否一致?

2.工程实践中的经验准则

线程对象生命周期 > 对应的线程生命周期

MyThread.h

复制代码
#ifndef MYTHREAD_H
#define MYTHREAD_H

#include <QThread>

class MyThread : public QThread
{
    Q_OBJECT

protected:
    int i;

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

#endif // MYTHREAD_H

MyThread.cpp

复制代码
#include "MyThread.h"

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

}
void MyThread::run()
{
    this->i = 1;
    for(int i=0; i<5; i++)
    {
        this->i *= 2;
        sleep(1);
    }
}

main.cpp

复制代码
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include "MyThread.h"
#include "SyncThread.h"

void test()
{
    MyThread t;
    t.start();
}

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

    return QCoreApplication::exec();
}

运行结果:

main() tid = 0x2030

QThread: Destroyed while thread is still running

因为t已经被销毁,但是t中的run函数仍在执行

3.同步型线程设计

  • 线程对象主动等待线程生命期结束后才销毁
  • 同时支持在栈和堆中创建线程对象
  • 对象销毁时确保线程生命期结束

要点:在析构函数中先调用wait()函数,强制等到线程运行结束

使用场合:线程生命期相对较短的情形

SyncThreadh

复制代码
#ifndef SYNCTHREAD_H
#define SYNCTHREAD_H

#include <QThread>

class SyncThread : public QThread
{
    Q_OBJECT
protected:
    void run();
public:
    explicit SyncThread(QObject *parent = nullptr);
    ~SyncThread();
};

#endif // SYNCTHREAD_H

SyncThread.cpp

复制代码
#include "SyncThread.h"
#include <QDebug>

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

}
void SyncThread::run()
{
    qDebug() << "void SyncThread::run() tid = " << currentThreadId();

    for(int i=0; i<3; i++)
    {
        qDebug() << "void SyncThread::run() i = " << i;
        sleep(1);
    }

    qDebug() << "void SyncThread::run() end";
}

SyncThread::~SyncThread()
{
    wait();
    qDebug() << "SyncThread::~SyncThread() destroy thread object";
}

main.cpp

复制代码
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include "MyThread.h"
#include "SyncThread.h"

void test()
{
    MyThread t;
    t.start();
}

void sync_thread()
{
    SyncThread st;
    st.start();
}

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

    return QCoreApplication::exec();
}

运行结果:

main() tid = 0xe84

void SyncThread::run() tid = 0x3430

void SyncThread::run() i = 0

void SyncThread::run() i = 1

void SyncThread::run() i = 2

void SyncThread::run() end

SyncThread::~SyncThread() destroy thread object

4.异步型线程设计

  • 在run()中最后调用deleteLater()函数
  • 线程体函数主动申请销毁线程对象
  • 使用场合:线程生命期不可控,需要长时间运行于后台的情形

注意:deleteLater() 只对堆对象有效,对栈对象无效

AsyncThread.h

复制代码
#ifndef ASYNCTHREAD_H
#define ASYNCTHREAD_H

#include <QThread>

class AsyncThread : public QThread
{

    Q_OBJECT

protected:
    void run();
public:
    explicit AsyncThread(QObject *parent = 0);
    ~AsyncThread();
};

#endif // ASYNCTHREAD_H

AsyncThread.cpp

复制代码
#include "AsyncThread.h"
#include <QDebug>

AsyncThread::AsyncThread(QObject *parent) : QThread(parent)
{

}
void AsyncThread::run()
{
    qDebug() << "void AsyncThread::run() tid = " << currentThreadId();
    for(int i=0; i<3; i++)
    {
        qDebug() << "void AsyncThread::run() i = " << i;
    }
    qDebug() << "void AsyncThread::run() end";
    deleteLater();
}
AsyncThread::~AsyncThread()
{
    qDebug() << "AsyncThread::~AsyncThread() destroy thread object";
}

main.cpp

复制代码
#include <QCoreApplication>
#include <QThread>
#include <QDebug>
#include "MyThread.h"
#include "SyncThread.h"
#include "AsyncThread.h"

void test()
{
    MyThread t;
    t.start();
}

void sync_thread()
{
    SyncThread st;
    st.start();
}

void asyns_thread()
{
    AsyncThread *at = new AsyncThread();
    at->start();
}

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

    return QCoreApplication::exec();
}

运行结果:

main() tid = 0x2cf4

void AsyncThread::run() tid = 0x130c

void AsyncThread::run() i = 0

void AsyncThread::run() i = 1

void AsyncThread::run() i = 2

void AsyncThread::run() end

AsyncThread::~AsyncThread() destroy thread object

相关推荐
小小龙学IT15 小时前
第七节 C++ 与 QML 交互
开发语言·qt
qq_4017004115 小时前
TCP 粘包问题深度解析:嵌入式网络开发的常见坑与解决方案
服务器·网络·qt·网络协议·tcp/ip
晓晓_za89866815 小时前
Geo 优化源码二次开发:自定义地域规则改造实操文档
java·开发语言·搜索引擎·ci/cd·矩阵
ShineWinsu16 小时前
对于TRAE中配置Qt的解析
开发语言·c++·ide·vscode·qt·ai·trae
wy31362282116 小时前
Git——ignore让项目的 target 目录真正被忽略(忽略其他文件也是同样的道理)
开发语言·git
格林威16 小时前
C#图像分块处理:图像按行或按块(Tile)切分,多个 CPU 核心同时处理不同的区域
开发语言·图像处理·人工智能·机器学习·计算机视觉·c#·工业相机
小小龙学IT16 小时前
第一节 QML 背景介绍
c++·qt·js
leisoo809716 小时前
财报舞弊预警系统实战用Python挖掘应收存货异常因子 IG50免费开源股票数据API接口
开发语言·python
君顾116 小时前
AI新零售线上商城系统实战:架构设计与开发全流程指南
java·开发语言·零售
wind1003216 小时前
Outdated Visual C++ Redistributable 过时 VC++ 运行库报错修复
开发语言·c++