Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Has every NSThread automatically a dispatch queue?

Has every thread an associated dispatch queue by default? I'm just wondering if I could use dispatch_semaphores in every context, or if i need to wrap it in an explicit dispatch call with a defined queue.

like image 921
Era Avatar asked Apr 08 '11 11:04

Era


1 Answers

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

like image 55
al45tair Avatar answered Oct 16 '22 16:10

al45tair