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

相关推荐
黄贵根1 小时前
JavaScript实现教培行业意向登记系统
开发语言·javascript·ecmascript
zzzll11114 小时前
Claude Code离线安装方案揭秘
开发语言·php
wling03015 小时前
vasp虚频计算-python脚本
开发语言·windows·python·vasp计算
郝学胜-神的一滴5 小时前
Qt 高级编程 040:按钮悬浮弹出滑块弹窗的完整攻略
开发语言·c++·qt·软件工程·用户界面
我是小灰灰吖6 小时前
Ubuntu 22.04 安装 SSH 服务与远程调试环境配置指南
qt·ubuntu·ssh
xrandzj6 小时前
Python面向对象编程入门:类、实例、初始化与封装实践
开发语言·python
DLYSB_7 小时前
API 网关流量洪峰与突发 CC 攻击:我用 Go 写了个“现场物理防御哨兵”,把故障响应压缩到秒级
开发语言·后端·golang·报警灯
雨田言炎9 小时前
四、关于Qt项目需要知道的
开发语言·笔记·qt
程序喵大人10 小时前
【C++进阶】STL算法与函数对象 - 02 sort为什么需要随机访问迭代器
开发语言·c++·算法
SomeB1oody10 小时前
【RustyML入门】2.6. 线性判别分析
开发语言·后端·机器学习·rust·教程