默认情况下,是DispatchQueue .serial还是.concurrent?

Sim*_*lGy 6 cocoa grand-central-dispatch

这就是问题,直截了当.

let serial = DispatchQueue(label: "serial", attributes: .serial)
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent)
let q = DispatchQueue(label: "q")
Run Code Online (Sandbox Code Playgroud)

我看不到任何可以检查的财产q会告诉我.

在带有PlaygroundPage.current.needsIndefiniteExecution = trueshow连续行为的游乐场中运行,但我不想依赖于操场(有异步的东西),或者没有文档的行为.

任何人都可以通过文档链接提供硬答案吗?

Ham*_*ish 8

此前斯威夫特3,默认调度队列类型是串行- 传递nil到属性参数dispatch_queue_create会产生一个串行队列,我认为没有理由为默认队列类型改变.虽然不幸的是我找不到任何DispatchQueue可以证实这一点的文件.

但是,查看源代码可以发现确实如此:

public convenience init(
    label: String,
    attributes: DispatchQueueAttributes = .serial, 
    target: DispatchQueue? = nil)
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

虽然我总是更喜欢明确指定属性,但是为了使我的代码更清晰并防止这种混淆.


rom*_*mex 6

UPD 斯威夫特 5

@Hamish 的答案是正确的:默认情况下,新的 DispatchQueue 是串行的。尽管源代码中属性的默认值不再像以前那样指定“.serial”:

public convenience init(
        label: String,
        qos: DispatchQoS = .unspecified,
        attributes: Attributes = [],
        autoreleaseFrequency: AutoreleaseFrequency = .inherit,
        target: DispatchQueue? = nil)
Run Code Online (Sandbox Code Playgroud)

值得注意的是,属性值集有.concurrent选项但不再有选项.serial。同样,默认情况下是串行的,如果在属性数组中明确分配,则是并发的。