dispatch_barrier_async不是带有dispatch_get_global_queue的单词

时间:2016-02-29 04:03:10

标签: ios objective-c grand-central-dispatch

有我的代码:

dispatch_queue_t queue =  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//    dispatch_queue_t queue = dispatch_queue_create("zxc", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
    NSLog(@"1");
});
dispatch_async(queue, ^{
    NSLog(@"3");
});
dispatch_async(queue, ^{
    NSLog(@"4");
});
dispatch_barrier_async(queue, ^{
    NSLog(@"write");
});
dispatch_async(queue, ^{
    NSLog(@"5");
});
dispatch_async(queue, ^{
    NSLog(@"2");
});
dispatch_async(queue, ^{
    NSLog(@"7");
});
dispatch_async(queue, ^{
    NSLog(@"6");
});

dispatch_barrier_async不适用于dispatch_get_global_queue,但可以使用dispatch_queue_create("zxc", DISPATCH_QUEUE_CONCURRENT);

为什么?

1 个答案:

答案 0 :(得分:2)

正确。您不能在全局队列上设置障碍。您只能在自己创建的私有队列上设置障碍。如dispatch_barrier_async的文档中所述:

  

您指定的队列应该是您使用dispatch_queue_create函数自己创建的并发队列。如果传递给此函数的队列是串行队列或全局并发队列之一,则此函数的行为类似于dispatch_async函数。