dispatch_sync
to dead lock?In GCD we add tasks to dispatch queues so that GCD can decide which thread to execute them on. Dispatch queues are thread-safe which means they can be accessed from different threads simultaneously. According to Apple's documentation, a dispatch queue is an object-like structure that manages the tasks you submit to it.
GCD is a low-level C-based API. NSOperation and NSOperationQueue are Objective-C classes. NSOperationQueue is objective C wrapper over GCD . If you are using NSOperation, then you are implicitly using Grand Central Dispatch.
The 64 thread width limit of GCD is undocumented because a well-designed concurrency architecture shouldn't depend on the queue width. You should always design your concurrency architecture such that a 2 thread wide queue would eventually finish the job to the same result (if slower) as a 1000 thread wide queue.
A message queue is a data structure for holding messages from the time they're sent until the time the receiver retrieves and acts on them. Generally queues are used as a way to 'connect' producers (of data) & consumers (of data). A thread pool is a pool of threads that do some sort of processing.
1. Does one thread only contain one queue?
The relationship is one way. Serial queue may hold a thread to execute the block dispatched to it, but thread doesn't know a queue. Well, main thread is special, it knows the main queue.
My guess
Dispatch queue doesn't indicate which thread it will run block or function on, I think dispatch queue manages a thread pool which contains many thread, it will fetch one idle thread when a block is dispatched. So one thread may work for many dispatch queue in a period time.
But one think is for sure: when you dispatch a block to a queue, the thread which this block is running on works for one determined dispatch queue, you can get it using dispatch_get_current_queue
.
2. If I dispatch a block asynchronously to globalQueue ,can it run on main thread by any chance?
I think it won't run any block to globalQueue on main thread, because it can't evaluate the execution time of the block, if it is a long time job, it will block the main thread.
3. what kind of situation will cause dispatch_sync to dead lock?
I reference the paragraph in Concurrency programming guide
You should never call the dispatch_sync or dispatch_sync_f function from a task that is executing in the same queue that you are planning to pass to the function. This is particularly important for serial queues, which are guaranteed to deadlock, but should also be avoided for concurrent queues.
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