在常用的SDWebImageDownloaderOperation类文件中我梦会看到dispatch_barrier_async(self.barrierQueue, ^{
[self.callbackBlocksaddObject:callbacks];
});对dispatch_barrier_async。那么我们如何理解dispatch_barrier_async通过下面这段代码的log时间可以很清晰的看到。


可以看到patch_barrier_async 效果类似 dispatch_async,区别就是中间多了一个barrier,barrier顾名思义就是屏障的意思,将队列一分为2,前面的代码执行完才能执行dispatch_barrier_async中的任务,最后执行队列后的任务。


可以看到patch_barrier_sync 效果类似 patch_barrier_async,都是将队列一分为2,前面的代码执行完才能执行dispatch_barrier_sync中的任务,最后执行队列后的任务。但是dispatch_barrier_sync是在主线程中执行,dispatch_barrier_async是在子线程中执行。如果有界面相关功能的代码,或者需要立即执行的代码,就可以使用dispatch_barrier_sync。其他情况均可用dispatch_barrier_async在子线程中完成。
网友评论