GCD

作者: crosstrack | 来源:发表于2017-05-18 17:15 被阅读93次
    并行 串行
    创建方法 dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL); dispatch_queue_create("com.yunding.locklock.ble", DISPATCH_QUEUE_SERIAL);
    系统默认 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).

    举个做饭的例子:
    我们要做一顿饭,这个任务可以细分为下面几个任务:

    1. 蒸米饭
      淘米、漫长的蒸米过程
    2. 做菜
      择菜、洗菜、切菜、切肉、腌肉、炒菜、闷热20秒
    3. 做完了叫人来吃饭

    好了,假设有好多小伙伴,可以一起来做来提高效率。
    米饭和菜可以同时来分开做,
    A择菜,B切肉,C淘米
    其中择菜、洗菜、切菜是有先后顺序的,
    切肉、腌肉也有顺序,淘米、蒸米也有顺序
    炒菜必须等菜和肉都到位才能进行

    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, ^{
             //洗菜,利用栅栏块来完成顺序执行
         });
        dispatch_barrier_async(queue2, ^{
            //切菜,利用栅栏块来完成顺序执行
        });
        
        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, ^{
            //洗菜,利用栅栏块来完成顺序执行
        });
        dispatch_group_async(group, queue2, ^{
            //切菜,利用栅栏块来完成顺序执行
        });
        
        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, ^{
            //材料准备完毕,炒菜
            
        });
        
    
    

    此外:
    dispatch_barrier_async的顺序执行还是依赖queue的类型啊,必需要queue的类型为dispatch_queue_create创建的,而且attr参数值必需是DISPATCH_QUEUE_CONCURRENT类型,dispatch_get_global_queue获得的并发队列被歧视了。
    延时执行dispatch_after
    执行n次(通过数组的形式封装任务,以实现group的效果)dispatch_apply(<#size_t iterations#>, <#dispatch_queue_t _Nonnull queue#>, <#^(size_t)block#>)
    执行1次 dispatch_once(<#dispatch_once_t * _Nonnull predicate#>, <#^(void)block#>)

    相关文章

      网友评论

          本文标题:GCD

          本文链接:https://www.haomeiwen.com/subject/prwbuttx.html