gpt4 book ai didi

cocoa - Cocoa 中等效的 C# 'Thread.Join()' 是什么?

转载 作者:行者123 更新时间:2023-12-03 16:17:44 25 4
gpt4 key购买 nike

我正在使用线程制作 iPhone 应用程序。我使用C#有一段时间了,有一个方法Thread.Join()可以阻止当前线程以等待特定线程完成执行。 cocoa 中的等价物是什么?或者替代方案?

---编辑---

PS。我正在使用 NSThread。

---编辑---

我发现像“waitForThreadExit”这样的方法会阻塞调用者线程,直到线程完成执行。

最佳答案

用 Cocoa 创建的线程不能被创建为分离的。出于资源管理的原因,NSThread 实例总是包装附加的 POSIX 线程。正如 Thread Programming Guide 中引用的:

If you do want to create joinable threads, the only way to do so is using POSIX threads. POSIX creates threads as joinable by default. To mark a thread as detached or joinable, modify the thread attributes using the pthread_attr_setdetachstate function prior to creating the thread. After the thread begins, you can change a joinable thread to a detached thread by calling the pthread_detach function. For more information about these POSIX thread functions, see the pthread man page. For information on how to join with a thread, see the pthread_join man page.

如果您正在寻找一种通知 NSThread 结束的方法,您可以使用 NSThreadWillExitNotification通知。

关于cocoa - Cocoa 中等效的 C# 'Thread.Join()' 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2216736/

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