queue有三種
Main: tasks execute serially on your application’s main thread
Concurrent: tasks start executing in FIFO order, but can run concurrently.
Serial: tasks execute one at a time in FIFO order
(1)serial queues(串行隊列)又稱私有排程隊列(private),一般用再對特定資源的同步通路上。我們可以根據需要建立任意數量的串行隊列,每一個串行隊列之間是并發的。
(2)并行隊列,又稱global dispatch queue。并行隊列雖然可以并發的執行多個任務,但是任務開始執行的順序和其加入隊列的順序相同。我們自己不能去建立并行排程隊列。隻有三個可用的global concurrent queues。
(3)main dispatch queue 是一個全局可用的串行隊列,其在行用程式的主線程上執行任務。此隊列的任務和應用程式的主循環(run loop)要執行的事件源交替執行。因為其運作在應用程式的主線程,main queue經常用來作為應用程式的一個同步點
Important functions in this C API
Creating and releasing queues
dispatch_queue_t dispatch_queue_create(const char *label, NULL); // serial queue void dispatch_release(dispatch_queue_t);
Putting blocks in the queue
typedef void (^dispatch_block_t)(void);
void dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
Getting the current or main queue
dispatch_queue_t dispatch_get_current_queue();
void dispatch_queue_retain(dispatch_queue_t); // keep it in the heap until dispatch_release
Main Queue: dispatch_queue_t dispatch_get_main_queue();
Global Queue: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
Serial: dispatch_queue_create("identifier",NULL);
Example ... assume we fetched an image from the network (this would be slow).
}
本文轉自老Zhan部落格園部落格,原文連結:http://www.cnblogs.com/mybkn/archive/2012/05/06/2486152.html,如需轉載請自行聯系原作者