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.
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()
:
“Queues are not bound to any specific thread of execution and blocks submitted to independent queues may execute concurrently.”
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With