QThread: Destroyed while thread is still running

Posted herd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了QThread: Destroyed while thread is still running相关的知识,希望对你有一定的参考价值。

Qt5已经分装了函数

void QThread::requestInterruption()
{
    Q_D(QThread);
    QMutexLocker locker(&d->mutex);
    if (!d->running || d->finished || d->isInFinish)
        return;
    if (this == QCoreApplicationPrivate::theMainThread) {
        qWarning("QThread::requestInterruption has no effect on the main thread");
        return;
    }
    d->interruptionRequested = true;
}
 
bool QThread::isInterruptionRequested() const
{
    Q_D(const QThread);
    QMutexLocker locker(&d->mutex);
    if (!d->running || d->finished || d->isInFinish) // 如果线程已经结束就。。。
        return false;
    return d->interruptionRequested;
}

在wile中用下面函数判断

while (!isInterruptionRequested())
{
     /////

     /////  
}

在析构函数中调用

ThreadToDisks:: ~ThreadToDisks()
{
    requestInterruption();
    quit();
    wait();
}

参考:https://blog.csdn.net/u013372900/article/details/80405261

 

以上是关于QThread: Destroyed while thread is still running的主要内容,如果未能解决你的问题,请参考以下文章

Qt笔记-解决QSslSocket中QWaitCondition: Destroyed while threads are still waiting问题

为啥qthread永远不会退出?

QThread 阻塞主应用程序

如何终止 QThread

Qt信号发射和QThread执行流程

Qthread 不工作,GUI 仍然挂起