每个 NSThread 都会自动分配一个调度队列吗?
默认情况下每个线程都有一个关联的调度队列吗?我只是想知道是否可以在每个上下文中使用dispatch_semaphore,或者是否需要将其包装在带有定义队列的显式调度调用中。
Has every thread an associated dispatch queue by default? I'm just wondering if I could use dispatch_semaphore
s in every context, or if i need to wrap it in an explicit dispatch call with a defined queue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它实际上并不像您的问题所暗示的那样起作用。默认情况下,有一个与主线程关联的主调度队列,以及三个并发的全局队列(分别为高优先级、默认优先级和低优先级)。
并发队列管理它们自己的线程资源,而不是与任何特定线程关联。
事实上,在
dispatch_queue_create()
的手册页上有非常明确的说明:至于是否可以在调度队列之外使用调度信号量(问题的另一部分),答案是肯定的,可以。它们是在马赫信号量之上实现的,并且应该在任何地方都可以工作。您可以在此处查看代码:
http://opensource。 apple.com/source/libdispatch/libdispatch-84.5.5/src/semaphore.c
It doesn’t really work the way your question implies. By default, there is a main dispatch queue associated with the main thread, and three global queues (high, default and low priorities respectively) that are concurrent.
Concurrent queues manage their own thread resources, rather than being associated with any particular thread.
In fact, it says quite specifically on the man page for
dispatch_queue_create()
:As regards whether you can use dispatch semaphores outside of dispatch queues (the other part of your question), the answer is yes, you can. They’re implemented on top of Mach semaphores, and should work everywhere. You can see the code here:
http://opensource.apple.com/source/libdispatch/libdispatch-84.5.5/src/semaphore.c