2012-06-26 99 views
3

我想了解隊列類型之間的差異。 據我瞭解有3種類型:GCD隊列類型

  • 全球隊列 - 並行 - 因爲他們提交
  • 私人塊被執行 - 串行 - 塊儘快不管爲了
  • 主隊列執行隊列 - 串行

我想知道的是: 當提交給每種隊列時,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. 
} 

我想知道我的這種看法有多少是正確的。

回答

2

dispatch_sync和dispatch_async之間的區別與windows API中的sendmessage和postmessage類似。 dispatch_sync 提交一個塊對象以便在調度隊列上執行並等待該塊完成。 dispatch_async 在調度隊列中提交異步執行塊並立即返回。

目標隊列確定該塊是相對於提交給同一隊列的其他塊而被串行調用還是同時調用。獨立的串行隊列相對於彼此同時處理。

在發佈問題之前,您應該先仔細閱讀文檔。