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

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM