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

后端 未结 1 1474
闹比i
闹比i 2021-02-10 03:43
  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 sit
1条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 04:46

    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.

    0 讨论(0)
提交回复
热议问题