gpt4 book ai didi

c++ - 在关闭应用程序期间正确关闭可能运行很长时间的线程

转载 作者:行者123 更新时间:2023-11-30 03:15:27 25 4
gpt4 key购买 nike

<分区>

因此,我有一个操作图像的类,即水平镜像或垂直镜像等。此类在其单独的线程中运行。

m_imageMirrorer = new ImageMirrorer();
m_imageMirrorer->moveToThread(&m_thread);
m_thread.start();
connect(&m_thread, &QThread::finished, this, &QObject::deleteLater);

现在,为了在用户随机关闭应用程序时正确关闭线程,我在析构函数中添加了以下代码。

    m_thread.quit();
if(!m_thread.wait(3000)) //Wait until it actually has terminated (max. 3 sec)
{
m_thread.terminate(); //Thread didn't exit in time, probably deadlocked, terminate it!
m_thread.wait(); //We have to wait again here!
}

if(m_imageMirrorer)
{
delete m_imageMirrorer;
}

实际上我最初尝试不使用 m_threadquit()wait()terminate()在析构函数中。但是它总是给出错误 QThread: Destroyed while thread is still runningdeleteLater 没有做任何事情来避免那个特定的崩溃。

我现在做的对吗?

谢谢。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com