Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the relationship between thread and queue in GCD?

Tags:

ios

  1. does one thread only contain one queue?
  2. if I dispatch a block asynchronously to globalQueue ,can it run on main thread by any chance?
  3. what kind of situation will cause dispatch_sync to dead lock?
like image 373
Xin Yuan Avatar asked Jul 15 '14 08:07

Xin Yuan


People also ask

What are thread-safe queues related to GCD?

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.

What is difference between operation queue and GCD?

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.

What is the thread limit of GCD?

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.

What is the difference between queue and thread?

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 Answers

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.

like image 175
KudoCC Avatar answered Nov 09 '22 03:11

KudoCC