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();
}

 

一定要先停掉线程run()中的while循环,然后再销毁指针,否则,就会卡主。

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

 

posted @ 2019-10-11 19:22  西北逍遥  阅读(3981)  评论(0编辑  收藏  举报