其他分享
首页 > 其他分享> > iOS GCD之Barrier

iOS GCD之Barrier

作者:互联网

Barrier

Calls to this function always return immediately after the block is submitted and never wait for the block to be invoked. When the barrier block reaches the front of a private concurrent queue, it is not executed immediately. Instead, the queue waits until its currently executing blocks finish executing. At that point, the barrier block executes by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes.

The queue you specify should be a concurrent queue that you create yourself using the dispatch_queue_create function. If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves like the dispatch_async function.
for (int i = 0; i < 5; i++) {
    dispatch_async(queue, ^{
        NSLog(@"%d前%@",i,[NSThread currentThread]);
    });
}

dispatch_barrier_async(queue, ^{
    sleep(10);
    NSLog(@"this is a barrier,%@",[NSThread currentThread]);
});

for (int i = 0; i < 5; i++) {
    dispatch_async(queue, ^{
        NSLog(@"%d后%@",i,[NSThread currentThread]);
    });
}

结果:
2022-02-24 22:32:48.840091+0800 iOSLearn[10911:190593] 1前<NSThread: 0x6000035755c0>{number = 4, name = (null)}
2022-02-24 22:32:48.840091+0800 iOSLearn[10911:190598] 0前<NSThread: 0x600003521b00>{number = 6, name = (null)}
2022-02-24 22:32:48.840122+0800 iOSLearn[10911:190592] 3前<NSThread: 0x600003570240>{number = 8, name = (null)}
2022-02-24 22:32:48.840156+0800 iOSLearn[10911:190594] 4前<NSThread: 0x60000356c1c0>{number = 3, name = (null)}
2022-02-24 22:32:48.840169+0800 iOSLearn[10911:190596] 2前<NSThread: 0x6000035386c0>{number = 7, name = (null)}
2022-02-24 22:32:58.845375+0800 iOSLearn[10911:190598] this is a barrier,<NSThread: 0x600003521b00>{number = 6, name = (null)}
2022-02-24 22:32:58.845874+0800 iOSLearn[10911:190598] 0后<NSThread: 0x600003521b00>{number = 6, name = (null)}
2022-02-24 22:32:58.845984+0800 iOSLearn[10911:190596] 1后<NSThread: 0x6000035386c0>{number = 7, name = (null)}
2022-02-24 22:32:58.846081+0800 iOSLearn[10911:190668] 2后<NSThread: 0x600003538200>{number = 9, name = (null)}
2022-02-24 22:32:58.846086+0800 iOSLearn[10911:190598] 3后<NSThread: 0x600003521b00>{number = 6, name = (null)}
2022-02-24 22:32:58.846169+0800 iOSLearn[10911:190669] 4后<NSThread: 0x600003578900>{number = 10, name = (null)}

标签:24,GCD,Barrier,0800,22,iOS,dispatch,queue,name
来源: https://blog.csdn.net/m0_58144709/article/details/123122773