zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    UGO小组冲刺第一天
    day04_07-三个函数的区别
    day06_08 字符串
    day06_07 字典操作02
    day06_06 字典操作01
    day06_05 字典
    day06_04 购物车讲解02
    day06_03 购物车讲解01
    day06_02 元组
    day06_01 上节回顾
  • 原文地址:https://www.cnblogs.com/herd/p/11656299.html
Copyright © 2011-2022 走看看