GCD

作者: NapoleonY | 来源:发表于2019-10-12 10:20 被阅读0次
    1. dispatch_barrier_async 与dispatch_barrier_sync
      添加栅栏,保证并行队列中 barrier 前面的任务执行完毕,才会执行 barrier 后面的任务
      区别:两者的区别即是 async 与 sync 的区别。
    • dispatch_barrier_async 所有的任务全部添加到队列中,barrier 中的队列执行完毕,再执行 barrier 任务,等待 barrier 任务执行完毕,最后执行 barrier 后面的任务(dispatch_barrier_async 结果中 dfafgda 与 ccccc 均先于 ******** barrier ****** 出现)
      dispatch_barrier_async 结果
    dfafgda , currentTime: 592539267.981731
    ccccc, currentTime: 592539267.981827
    ffff 444444, currentTime: 592539267.981756
    ffff 111111, currentTime: 592539267.981731
    ffff 222222, currentTime: 592539267.981732
    ffff 333333, currentTime: 592539267.981731
    ffff 111111_bbbbbbbb, currentTime: 592539269.986992
    ******** barrier ******, currentTime: 592539269.987095
    ******** barrier_2 ******, currentTime: 592539271.992194
    bbbb 5555555, currentTime: 592539271.992281
    bbbb 6666666, currentTime: 592539271.992298
    bbbb 7777777, currentTime: 592539271.992321
    bbbb 8888888, currentTime: 592539271.992325
    
    • dispatch_barrier_sync 先将 barrier 前面的任务添加到队列中,阻塞代码所在的队列,等待这些任务执行完毕,然后添加 barrier 任务(dispatch_barrier_sync 结果中根据时间可以看到 ffff 111111_bbbbbbbb 出现以后,才出现了 ******** barrier ******),等待 barrier 任务执行完毕,然后运行 barrier 后面的代码(根据时间可以看到 dfafgda 与 ccccc 均在 ******** barrier_2 ****** 以后出现)
    private func barrierTest() {
            let queue = DispatchQueue.init(label: "aaa", qos: .default, attributes: .concurrent)
            queue.async {
                print("ffff 111111, currentTime: \(CFAbsoluteTimeGetCurrent())")
                sleep(2)
                print("ffff 111111_bbbbbbbb, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            queue.async {
                print("ffff 222222, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            queue.async {
                print("ffff 333333, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            queue.async {
                print("ffff 444444, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
    //        queue.async(qos: .default, flags: .barrier) {
    //            print("******** barrier ******, currentTime: \(CFAbsoluteTimeGetCurrent())")
    //            sleep(2)
    //            print("******** barrier_2 ******, currentTime: \(CFAbsoluteTimeGetCurrent())")
    //        }
            queue.sync(flags: .barrier) { () -> Int in
                print("******** barrier ******, currentTime: \(CFAbsoluteTimeGetCurrent())")
                sleep(2)
                print("******** barrier_2 ******, currentTime: \(CFAbsoluteTimeGetCurrent())")
                return 1
            }
            queue.async {
                print("bbbb 5555555, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            print("dfafgda , currentTime: \(CFAbsoluteTimeGetCurrent())")
            queue.async {
                print("bbbb 6666666, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            queue.async {
                print("bbbb 7777777, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            queue.async {
                print("bbbb 8888888, currentTime: \(CFAbsoluteTimeGetCurrent())")
            }
            print("ccccc, currentTime: \(CFAbsoluteTimeGetCurrent())")
        }
    

    dispatch_barrier_sync 结果

    ffff 111111, currentTime: 592538837.188629
    ffff 222222, currentTime: 592538837.188665
    ffff 444444, currentTime: 592538837.188673
    ffff 333333, currentTime: 592538837.188669
    ffff 111111_bbbbbbbb, currentTime: 592538839.193317
    ******** barrier ******, currentTime: 592538839.193441
    ******** barrier_2 ******, currentTime: 592538841.194509
    dfafgda , currentTime: 592538841.194632
    ccccc, currentTime: 592538841.194643
    bbbb 5555555, currentTime: 592538841.194689
    bbbb 6666666, currentTime: 592538841.194704
    bbbb 7777777, currentTime: 592538841.194708
    bbbb 8888888, currentTime: 592538841.194712
    

    相关文章

      网友评论

          本文标题:GCD

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