GCD

2019-10-12  本文已影响0人  NapoleonY
  1. dispatch_barrier_async 与dispatch_barrier_sync
    添加栅栏,保证并行队列中 barrier 前面的任务执行完毕,才会执行 barrier 后面的任务
    区别:两者的区别即是 async 与 sync 的区别。
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
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
上一篇下一篇

猜你喜欢

热点阅读