等待NSThread

我有很多NSThreads,我想在他们工作的时候睡觉。我该怎么做?在iOS SDK中是否存在WinApi函数WaitForSingleObject / WaitForMultipleObjects的模拟?     
已邀请:
有很多方法,但我的主要建议是研究使用libdispatch。 而不是产生NSThreads做:
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    /* work to do in a thread goes here */
});
/* repeat for other threads */
dispatch_group_wait(group, DISPATCH_TIME_FOREVER); //wait for all the async tasks in the group to complete
有关文档,请参阅http://developer.apple.com/library/mac/#documentation/Darwin/Reference/ManPages/man3/dispatch_group_async.3.html。 另一种方法是使用信号量,posix或dispatch(http://www.csc.villanova.edu/~mdamian/threads/posixsem.html有一些信息,http://developer.apple.com/也是如此)库/ IOS /#文档/一般/概念/ ConcurrencyProgrammingGuide / OperationQueues / OperationQueues.html)。 (编辑后再添加一个替代方案): 如果你所有的线程基本上都做同样的工作(即分割任务而不是做一堆不同的任务),这也可以很好地工作,并且更简单:
dispatch_apply(count, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(size_t i){
 doWork(someData, i);
});
    
听起来你应该重新考虑你的应用程序的架构。拥有许多线程,特别是在iOS上,几乎可以保证比较简单的设计更慢,而且绝对比较笨拙。 在iOS上,只有一个核心,总​​线带宽非常有限。 尽可能使用更高级别的系统提供的并发工具(NSOperation,dispatch和任何异步API)会更好。     

要回复问题请先登录注册