美文网首页
dispatch_barrier_sync和dispatch_b

dispatch_barrier_sync和dispatch_b

作者: Smallwolf_JS | 来源:发表于2020-04-04 19:29 被阅读0次

写在最前面:
在使用栅栏函数时.使用自定义队列才有意义,如果用的是串行队列或者系统提供的全局并发队列,这个栅栏函数的作用等同于一个同步函数的作用

下面是测试执行代码,看过代码应该就知道区别了.

dispatch_queue_t queue = dispatch_queue_create("com.shen.thread.demo", DISPATCH_QUEUE_CONCURRENT);
     NSLog(@"start");
     dispatch_async(queue, ^{
         NSLog(@"currentThread-1:%@", [NSThread currentThread]);
     });
     dispatch_barrier_async(queue, ^{
         NSLog(@"currentThread-2:%@", [NSThread currentThread]);
         [NSThread sleepForTimeInterval:2];
     });
     NSLog(@"pause");
     dispatch_async(queue, ^{
         NSLog(@"currentThread-3:%@", [NSThread currentThread]);
     });
     NSLog(@"end");

这是异步的执行顺序


     dispatch_queue_t queue = dispatch_queue_create("com.shen.thread.demo", DISPATCH_QUEUE_CONCURRENT);
     NSLog(@"start");
     dispatch_async(queue, ^{
         NSLog(@"currentThread-1:%@", [NSThread currentThread]);
     });
     dispatch_barrier_sync(queue, ^{
         NSLog(@"currentThread-2:%@", [NSThread currentThread]);
         [NSThread sleepForTimeInterval:2];
     });
     NSLog(@"pause");
     dispatch_async(queue, ^{
         NSLog(@"currentThread-3:%@", [NSThread currentThread]);
     });
     NSLog(@"end");

相关文章

网友评论

      本文标题:dispatch_barrier_sync和dispatch_b

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