<一>什么是dispatch_barrier_async函數
毫無疑問,dispatch_barrier_async函數的作用與barrier的意思相同,在進程管理中起到一個柵欄的作用,它等待所有位於barrier函數之前的操作執行完畢后執行,並且在barrier函數執行之后,barrier函數之后的操作才會得到執行,該函數需要同dispatch_queue_create函數生成的concurrent Dispatch Queue隊列一起使用
<二>dispatch_barrier_async函數的作用
1.實現高效率的數據庫訪問和文件訪問
2.避免數據競爭
<三>dispatch_barrier_async實例
- (void)barrier {
//同dispatch_queue_create函數生成的concurrent Dispatch Queue隊列一起使用 dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ NSLog(@"----1-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----2-----%@", [NSThread currentThread]); }); dispatch_barrier_async(queue, ^{ NSLog(@"----barrier-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----3-----%@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"----4-----%@", [NSThread currentThread]); }); }
輸出結果:1 2 --> barrier -->3 4 其中12 與 34 由於並行處理先后順序不定