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

相关推荐
影寂ldy1 小时前
C# 多线程进阶知识点(线程优先级、多委托传参、线程锁、死锁)
开发语言·数据库·c#
国服第二切图仔1 小时前
09-搜索工具Grep与Glob
开发语言·javascript·ecmascript
凤山老林1 小时前
从“无法创建本地线程“异常说起:深度解析线程栈与JVM线程优化指南
java·开发语言·jvm
库克克1 小时前
【C++】多态
开发语言·c++
观远数据2 小时前
经营闭环的最后一公里:从看见问题到解决问题之间还差什么
java·开发语言·数据库
Java面试题总结2 小时前
Go 语言大白话入门 6 - 判断与循环
开发语言·后端·golang
m0_617493942 小时前
Python OpenCV 孔洞检测与缺陷分析详解与实战
开发语言·python·opencv
hsg773 小时前
简述:Rust、GeoRust、自主研发GIS平台
开发语言·后端·rust
承渊政道3 小时前
飞算Java炫技赛:Java×Unity数字孪生园区平台的从零落地记录
java·开发语言·unity·技术分享·vibe coding·飞算javaai·飞算java ai炫技赛