GCD队列类型

时间:2012-06-26 14:44:08

标签: grand-central-dispatch

我正在尝试了解队列类型之间的差异。 据我了解,有3种类型:

  • 全局队列 - 并发 - 块尽快执行,无论顺序如何
  • 主要队列 - 序列块在提交时执行
  • 专用队列 - 序列号

我想知道的是: 提交到每种队列时,dispatch_sync和dispatch_async之间有什么区别? 到目前为止,这就是我的理解:

dispatch_sync(global_queue)^
{
     // blocks are executed one after the other in no particular order
     // example: block 3 executes. when it finishes block 7 executes.
}

dispatch_async(global_queue)^
{
     // blocks are executed concurrently in no particular order
     // example: blocks 2,4,5,7 execute at the same time.
}

dispatch_sync(main_queue)^
{
     // blocks are executed one after the other in the order they were submitted
     // example: block 1 executes. when it finishes block 2 will execute and so forth.
}

dispatch_async(main_queue)^
{
     // blocks are executed concurrently in the order they were submitted
     // example: blocks 1-4 (or whatever amount of threads the system can handle at one time) will fire at the same time. 
     // when the first block completes block 5 will then execute.
}

我想知道我对此的看法是多么正确。

1 个答案:

答案 0 :(得分:2)

dispatch_sync和dispatch_async之间的区别与windows API中的sendmessage和postmessage类似。 dispatch_sync 提交一个块对象以便在调度队列上执行,并等待该块完成。 dispatch_async 在调度队列上提交异步执行块并立即返回。

目标队列确定是否相对于提交到同一队列的其他块串行或同时调用该块。独立的串行队列彼此同时处理。

在发布问题之前,你应该先仔细阅读文件。