GCD

并行 串行
創(chuàng)建方法 dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL); dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL);
系統(tǒng)默認(rèn) dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_get_main_queue()
dispatch_queue_t
dispatch_queue_create(const char *_Nullable label,
        dispatch_queue_attr_t _Nullable attr);

/*!
 * @typedef dispatch_queue_attr_t
 *
 * @abstract
 * Attribute for dispatch queues.
 */
DISPATCH_DECL(dispatch_queue_attr);

/*!
 * @const DISPATCH_QUEUE_SERIAL
 *
 * @discussion A dispatch queue that invokes blocks serially in FIFO order.
 */
#define DISPATCH_QUEUE_SERIAL NULL

/*!
 * @const DISPATCH_QUEUE_SERIAL_INACTIVE
 *
 * @discussion
 * A dispatch queue that invokes blocks serially in FIFO order, and that is
 * created initially inactive. See dispatch_queue_attr_make_initially_inactive().
 */
#define DISPATCH_QUEUE_SERIAL_INACTIVE \
        dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_SERIAL)

/*!
 * @const DISPATCH_QUEUE_CONCURRENT
 *
 * @discussion A dispatch queue that may invoke blocks concurrently and supports
 * barrier blocks submitted with the dispatch barrier API.
 */
#define DISPATCH_QUEUE_CONCURRENT \
        DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, \
        _dispatch_queue_attr_concurrent)
__OSX_AVAILABLE_STARTING(__MAC_10_7,__IPHONE_4_3)
DISPATCH_EXPORT
struct dispatch_queue_attr_s _dispatch_queue_attr_concurrent;

/*!
 * @const DISPATCH_QUEUE_CONCURRENT_INACTIVE
 *
 * @discussion
 * A dispatch queue that may invoke blocks concurrently and supports barrier
 * blocks submitted with the dispatch barrier API, and that is created initially
 * inactive. See dispatch_queue_attr_make_initially_inactive().
 */
#define DISPATCH_QUEUE_CONCURRENT_INACTIVE \
        dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_CONCURRENT)


dispatch_queue_attr_t 宏定義
串行 DISPATCH_QUEUE_SERIAL NULL
DISPATCH_QUEUE_SERIAL_INACTIVE dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_SERIAL)
DISPATCH_QUEUE_CONCURRENT DISPATCH_GLOBAL_OBJECT(dispatch_queue_attr_t, _dispatch_queue_attr_concurrent)
DISPATCH_QUEUE_CONCURRENT_INACTIVE dispatch_queue_attr_make_initially_inactive(DISPATCH_QUEUE_CONCURRENT)
dispatch_queue_t
dispatch_get_global_queue(long identifier, unsigned long flags);
 * well-known global concurrent queues:
 *  - QOS_CLASS_USER_INTERACTIVE
 *  - QOS_CLASS_USER_INITIATED
 *  - QOS_CLASS_DEFAULT
 *  - QOS_CLASS_UTILITY
 *  - QOS_CLASS_BACKGROUND
 *
 * The global concurrent queues may still be identified by their priority,
 * which map to the following QOS classes:
 *  - DISPATCH_QUEUE_PRIORITY_HIGH:         QOS_CLASS_USER_INITIATED
 *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:      QOS_CLASS_DEFAULT
 *  - DISPATCH_QUEUE_PRIORITY_LOW:          QOS_CLASS_UTILITY
 *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND:   QOS_CLASS_BACKGROUND
 *
 * @param flags
 * Reserved for future use. Passing any value other than zero may result in
 * a NULL return value.

dispatch_queue_priority_t 宏定義
DISPATCH_QUEUE_PRIORITY_HIGH 2 * Items dispatched to the queue will run at high priority, the queue will be scheduled for execution before any default priority or low priority queue.
DISPATCH_QUEUE_PRIORITY_DEFAULT 0 * Items dispatched to the queue will run at the default priority, i.e. the queue will be scheduled for execution after all high priority queues have been scheduled, but before any low priority queues have been scheduled.
DISPATCH_QUEUE_PRIORITY_LOW -2
DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN * Items dispatched to the queue will run at background priority, i.e. the queue will be scheduled for execution after all higher priority queues have been scheduled and the system will run items on this queue on a thread with background status as per setpriority(2) (i.e. disk I/O is throttled and the thread's scheduling priority is set to lowest value).

舉個(gè)做飯的例子:
我們要做一頓飯,這個(gè)任務(wù)可以細(xì)分為下面幾個(gè)任務(wù):

  1. 蒸米飯
    淘米、漫長(zhǎng)的蒸米過(guò)程
  2. 做菜
    擇菜、洗菜、切菜、切肉、腌肉、炒菜、悶熱20秒
  3. 做完了叫人來(lái)吃飯

好了,假設(shè)有好多小伙伴,可以一起來(lái)做來(lái)提高效率。
米飯和菜可以同時(shí)來(lái)分開(kāi)做,
A擇菜,B切肉,C淘米
其中擇菜、洗菜、切菜是有先后順序的,
切肉、腌肉也有順序,淘米、蒸米也有順序
炒菜必須等菜和肉都到位才能進(jìn)行

dispatch_queue_t queue1 = dispatch_queue_create("米", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue2 = dispatch_queue_create("菜", DISPATCH_QUEUE_CONCURRENT);
    dispatch_queue_t queue3 = dispatch_queue_create("肉", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue4 = dispatch_queue_create("炒菜", DISPATCH_QUEUE_SERIAL);

    dispatch_async(queue1, ^{
        //淘米
    });
    dispatch_async(queue1, ^{
        //蒸米
    });
    
    dispatch_async(queue2, ^{
        //擇菜,有好多菜,可以分頭擇,并行
    });
     dispatch_barrier_async(queue2, ^{
         //洗菜,利用柵欄塊來(lái)完成順序執(zhí)行
     });
    dispatch_barrier_async(queue2, ^{
        //切菜,利用柵欄塊來(lái)完成順序執(zhí)行
    });
    
    dispatch_async(queue3, ^{
        //洗肉
    });
    dispatch_async(queue3, ^{
        //腌肉
    });
    dispatch_async(queue3, ^{
        //切肉
    });
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue2, ^{
       //擇菜,有好多菜,可以分頭擇,并行
    });
    dispatch_group_async(group, queue2, ^{
        //洗菜,利用柵欄塊來(lái)完成順序執(zhí)行
    });
    dispatch_group_async(group, queue2, ^{
        //切菜,利用柵欄塊來(lái)完成順序執(zhí)行
    });
    
    dispatch_group_async(group, queue3, ^{
        //洗肉
    });
    dispatch_group_async(group, queue3, ^{
        //腌肉
    });
    dispatch_group_async(group, queue3, ^{
        //切肉
    });
    
    dispatch_group_wait(group, 20*60);//同步等待完成
    dispatch_group_notify(group, queue4, ^{
        //材料準(zhǔn)備完畢,炒菜
        
    });
    

此外:
dispatch_barrier_async的順序執(zhí)行還是依賴queue的類型啊,必需要queue的類型為dispatch_queue_create創(chuàng)建的,而且attr參數(shù)值必需是DISPATCH_QUEUE_CONCURRENT類型,dispatch_get_global_queue獲得的并發(fā)隊(duì)列被歧視了。
延時(shí)執(zhí)行dispatch_after
執(zhí)行n次(通過(guò)數(shù)組的形式封裝任務(wù),以實(shí)現(xiàn)group的效果)dispatch_apply(<#size_t iterations#>, <#dispatch_queue_t _Nonnull queue#>, <#^(size_t)block#>)
執(zhí)行1次 dispatch_once(<#dispatch_once_t * _Nonnull predicate#>, <#^(void)block#>)

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容