gpt4 book ai didi

objective-c - 分离的 NSThreads 总是会在应用程序退出之前完成吗?

转载 作者:行者123 更新时间:2023-12-03 17:41:08 24 4
gpt4 key购买 nike

当使用NSThreaddetachNewThreadSelector:toTarget:withObject:时,我发现如果用户是在后台进程执行时尝试退出应用程序。

在本例中,这是我想要的行为,但我在 Apple 文档中找不到任何表明情况始终如此的内容。我能找到的唯一相关信息如下,来自 Apple 的 Threading Programming Guide :

Important: At application exit time, detached threads can be terminated immediately but joinable threads cannot. Each joinable thread must be joined before the process is allowed to exit. Joinable threads may therefore be preferable in cases where the thread is doing critical work that should not be interrupted, such as saving data to disk.

因此,我知道分离线程可以在应用程序退出时终止,但它们会自动终止吗?或者,我是否总是可以安全地假设线程将在应用程序退出之前完成其执行?

最佳答案

无论文档如何,您都不能假设任何线程(包括主线程)都会正常完成执行。

这是因为用户可以随时退出应用程序,系统可能会断电/崩溃,或者应用程序可能会崩溃。

对于分离线程来说,一旦主事件循环消失,系统框架会在超时后自动强制终止应用程序,这并非闻所未闻。

关于objective-c - 分离的 NSThreads 总是会在应用程序退出之前完成吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14732609/

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