线程何时退出? [英] When does a thread exit?

查看:27
本文介绍了线程何时退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Qt 中有这样的代码:

I have code in Qt that goes like this:

void doSomeStuff()
{
    ...
    if(blah blah)
        someObj.start();
    ...
}

然后someObj.run():

void someObj::run()
{
    blah blah blah, do some stuff
    doSomeStuff()
}

打算是让在 someObj.start() 创建的新线程结束,然后函数 doSomeStuff 在主一旦停止,线程就会被再次调用.但是我的程序是随机崩溃的,当我再次密切关注代码时,我意识到可能真实的情况是新线程只是调用函数而没有死亡,从而不断产生新线程.我的怀疑是否正确?如果是这样,我应该如何补救?

What I was intending is for the new thread created at someObj.start() to end and then the function doSomeStuff in the main thread gets called again once it stops. But my program was crashing randomly, and when I closely followed the code again, I realized that perhaps the real case was that the new thread merely calls the function without dying off, thus spawning new threads continuously. Are my suspicions correct? If so, how should I remedy this?

推荐答案

doSomeStuff() 当前在线程执行时被调用 ego 线程仍然是活动的,否则它根本不会被调用 ^^ 只有当 doSomeStuff 完成时那么你的线程将退出

doSomeStuff() is currently called when the thread is exectured ego the thread will still be alive otherwise it would not be called at all ^^ only when doSomeStuff is finished then your Thread will exit

通过浏览 QT 文档(我不是 Qt 专家)我发现

By glancing at the QT doc (I'm no Qt expert) I found

http://doc.qt.io/qt-5/qthread.html

bool isFinished() const

bool isFinished () const

你可以说从那里回电

这篇关于线程何时退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆