首页| 论坛| 消息

标题:QThread isRunning和isFinished
作者:lwei24
日期:2022-11-25 15:40
内容:

如题,在QWidget的派生类的构造函数中new一个线程对象,具体代码如下:|
m_thread = new QThread(this);
因为这个派生类有一个关闭按钮,我想在关闭按钮的时候终止线程,但为啥m_thread还没有start,直接关闭按钮,即
各位大佬为啥线程没有start(),m_thread->isRunning()返回false说明线程没有运行,m_thread->isFinished()返回false,说明线程还没结束?欢迎各位大佬指点一二,小弟在此多谢了!qWarning()


#1 [zoubing 11-28 22:55]
/*!
Returns true if the thread is finished; otherwise returns false.
\sa isRunning()
*/
bool QThread::isFinished() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
return d->finished;
}
/*!
Returns true if the thread is running; otherwise returns false.
\sa isFinished()
*/
bool QThread::isRunning() const
{
Q_D(const QThread);
QMutexLocker locker(&d->mutex);
#ifdef Q_OS_SYMBIAN
// app shutdown on Symbian can terminate threads and invalidate their stacks without notification,
// check the thread is still alive.
if (d->data->symbian_thread_handle.Handle() && d->data->symbian_thread_handle.ExitType() != EExitPending)
return false;
#endif
return d->running;
}
都在源码里边了,可以理解为QThread里边有俩成员变量(非直接成员,使用d指针包装了),一个是bool running,一个是bool finished;其中running在start时置为true,finished置为false,,线程结束时类似处理...

回复 发表
主题 版块