并发队列上的`.barrier`是否应立即生效?

Sim*_*lGy 5 grand-central-dispatch swift swift3 xcode8

我不确定这是否打破了xcode8 beta 5.看看这段代码.你认为首先应该打印"A",还是首先打印"B"?

let q = DispatchQueue(label: "q", attributes: .concurrent)
q.async(flags: .barrier) {
  Thread.sleep(forTimeInterval: 0.25)
  print("A")
}
q.sync {
  print("B")
}
Run Code Online (Sandbox Code Playgroud)

因为.barrier,我认为它应该阻止并发队列并打印"A","B",但它不在最新的xcode beta中.

错误?对.barrier的误解?你怎么看?

请注意:我知道如果我使用串行队列,这将以预期的顺序打印 - 这是一个更大的系统的一小部分,我需要孤立地理解这种行为.

Rob*_*Rob 5

这是测试版6中修复的错误.

在测试版5中,它不仅没有按照您的预期行事,而且.onQueueAsBarrier前提条件也失败了.这个问题似乎被埋没在async.barrier选项,因为如果你通过等价的Objective-C API执行它,dispatch_barrier_async,它工作正常,如:

let q = DispatchQueue(label: "q", attributes: .concurrent)

BarrierExperiment.dispatchBarrierAsync(q) {
    dispatchPrecondition(condition: .onQueueAsBarrier(q))
    Thread.sleep(forTimeInterval: 0.25)
    print("A")
}

q.async() {
    print("B")
}
Run Code Online (Sandbox Code Playgroud)

哪里

@interface BarrierExperiment : NSObject

+ (void)dispatchBarrierAsync:(dispatch_queue_t)queue block:(void (^)())block;

@end

@implementation BarrierExperiment

+ (void)dispatchBarrierAsync:(dispatch_queue_t)queue block:(void (^)())block {
    dispatch_barrier_async(queue, block);
}

@end
Run Code Online (Sandbox Code Playgroud)