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

相关推荐
吃我一窝jio4 小时前
Python与Go线程模型深度对比
java·开发语言·python
eggwyw4 小时前
C++ union 的一种妙用法
开发语言·c++
ShineWinsu6 小时前
对于Qt:QHBoxLayout常见属性以及方法的解析
c++·qt·面试·gui·客户端·布局管理器·水平布局器
starzy19906 小时前
深入理解Scala隐式函数:自动类型转换
开发语言·后端·scala
布莱克6058 小时前
Swift 循环与控制转移详解:从 for-in 到标签跳转
开发语言·ssh·swift·循环
敲上瘾9 小时前
Qt核心机制:信号与槽详解
开发语言·c++·qt·ui·qt6.3
联犀9 小时前
ai时代复杂项目的golang项目如何更好的与ai共舞:monorepo+gozero
开发语言·人工智能·golang
lcj251111 小时前
[C++] C++11
开发语言·c++·笔记
Evand J13 小时前
【MATLAB例程】基于IMM-CKF的AOA+测距融合的二维移动目标跟踪定位
开发语言·matlab·目标跟踪
JianZhen✓14 小时前
IM 聊天列表为什么总“消息乱跳“:有序插入、时间轴派生、本地截断、已读回执
开发语言·前端·php