简体   繁体   English

退出进程时等待线程完成

[英]Waiting on threads to finish while quitting the process

There was no direct and satisfactory answer found on quite a simple question: 在一个简单的问题上没有找到直接令人满意的答案:

Given multiple threads running is there a generic/correct way to wait on them to finish while exiting the process? 给定多个线程正在运行,是否存在一种通用/正确的方法来等待它们在退出进程时完成? Or "is doing timed wait Ok in this case?" 还是“在这种情况下是否可以定时等待?”

Yes, we attempt to signal threads to finish but it is observed that during process exit some of them tend to stall. 是的,我们尝试向线程发出结束信号,但可以观察到,在进程退出期间,其中一些线程趋于停顿。 We recently had a discussion and it was decided to rid of "arbitrary wait": 我们最近进行了讨论,因此决定摆脱“任意等待”:

m_thread.quit();          // the way we had threads finished
m_thread.wait(kWaitMs);   // with some significant expiration (~1000ms)

m_thread.quit();          // the way we have threads finished now
m_thread.wait();          // wait forever until finished

I understand that kWaitMs constant should be chosen somewhat proportional to one uninterrupted "job cycle" for the thread to finish. 我知道应该选择kWaitMs常数,该常数与一个不间断的“作业周期”成正比,以使线程完成。 Say, if the thread processes some chunk of data for 10 ms then we should probably wait on it to respond to quit signal for 100 ms and if it still does not quit then we just don't wait anymore. 假设,如果线程在10毫秒内处理某些数据块,那么我们可能应该等待100毫秒以对退出信号做出响应,如果它仍然不退出,那么我们就不再等待。 We don't wait in that case as long as we quit the program and no longer care. 只要退出程序并且不再关心,我们就不会在这种情况下等待。 But some engineers don't understand such "paradigm" and want an ultimate wait. 但是有些工程师不理解这种“范式”,而是希望最终等待。 Mind that the program process stuck in memory on the client machine will cause problems on the next program start in our case for sure not to mention that the log will not be properly finished to process as an error. 请注意,在本例中,卡在客户端计算机内存中的程序进程将在下次启动程序时引起问题,以确保不要提及日志将无法正确完成以作为错误进行处理。

Can the question about the proper thread finishing on process quit be answered? 是否可以回答有关退出进程的正确线程的问题?

Is there some assistance from Qt/APIs to resolve the thread hang-up better, so we can log the reason for it? Qt / API是否提供了一些帮助来更好地解决线程挂起的问题,所以我们可以记录下原因了吗?

PS Mind that I am well aware on why it is wrong to terminate the thread forcefully and how can that be done. PS Mind,我很清楚为什么强制终止线程是错误的,以及如何做到这一点。 This question I guess is not about synchronization but about limited determinism of threads that run tons of our and framework and OS code. 我猜这个问题不是关于同步,而是关于运行大量我们代码以及框架和OS代码的线程的有限确定性。 The OS is not Real Time, right: Windows / MacOS / Linux etc. 操作系统不是实时的,对:Windows / MacOS / Linux等。

PPS All the threads in question have event loop so they should respond to QThread::quit() . PPS所有有问题的线程都有事件循环,因此它们响应QThread::quit()

Yes, we attempt to signal threads to finish but it is observed that during process exit some of them tend to stall. 是的,我们尝试向线程发出结束信号,但可以观察到,在进程退出期间,其中一些线程趋于停顿。

That is your real problem. 那是你真正的问题。 You need to figure out why some of your threads are stalling, and fix them so that they do not stall and always quit reliably when they are supposed to. 您需要弄清楚为什么某些线程会停顿,并对其进行修复,以使它们不会停顿,并始终在应有的情况下可靠地退出。 (The exact amount of time they take to quit isn't that important, as long as they do quit in a reasonable amount of time, ie before the user gets tired of waiting and force-quits the whole application) (他们退出的确切时间并不重要,只要他们在合理的时间内退出即可,即在用户厌倦等待并强制退出整个应用程序之前)

If you don't/can't do that, then there is no way to shut down your app reliably, because you can't safely free up any resources that a thread might still be accessing. 如果不这样做,则无法可靠地关闭应用程序,因为您无法安全地释放线程可能仍在访问的任何资源。 It is necessary to 100% guarantee that a thread has exited before the main thread calls the destructors of any objects that the thread uses (eg the QThread object associated with the thread) 必须100%保证在主线程调用该线程使用的任何对象的析构函数之前已退出线程(例如,与该线程关联的QThread对象)

So to sum up: don't bother playing games with wait-timeouts or forcibly-terminating threads; 综上所述:不要因为等待超时或强行终止线程而打扰游戏; all that will get you is an application that sometimes crashes on shutdown. 所得到的只是一个有时会在关机时崩溃的应用程序。 Use an indefinite-wait, and make sure your threads always (always!) quit after the main thread has asked them to, as that is the only way you'll achieve a reliable shutdown sequence. 使用无限期等待,并确保在主线程请求线程之后总是(总是!)退出线程,因为这是实现可靠关闭顺序的唯一方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM