2016-02-29 50 views
-1

字有我的代碼:dispatch_barrier_async不dispatch_get_global_queue

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_asyncdispatch_get_global_queue工作,但可以用dispatch_queue_create("zxc", DISPATCH_QUEUE_CONCURRENT);

爲什麼工作?

+2

你是什麼意思的「不起作用」?什麼是實際問題?請明確點。 – rmaddy

回答

2

正確。您不能在全局隊列上放置障礙。你只能在你自己創建的私人隊列上設置障礙。正如dispatch_barrier_async文檔中所述:

您指定的隊列應該是使用dispatch_queue_create函數創建的併發隊列。如果您傳遞給此函數的隊列是串行隊列或全局併發隊列之一,則此函數的行爲與dispatch_async函數相同。

相關問題