默认情况下,DispatchQueue 是.serial 还是.concurrent?
By default, is DispatchQueue .serial or .concurrent?
这就是问题,直截了当。
let serial = DispatchQueue(label: "serial", attributes: .serial)
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent)
let q = DispatchQueue(label: "q")
我看不到 属性 我可以检查 q
会告诉我。
运行 in playground with PlaygroundPage.current.needsIndefiniteExecution = true
显示串行行为,但我不想依赖 playground(有点像异步东西)或未记录的行为。
任何人都可以提供 link 文档的硬性答案吗?
在 Swift 3 之前,默认的调度队列类型是串行的——passing nil
into the attributes parameter of dispatch_queue_create
会产生一个串行队列,我看不出默认队列类型是改变。虽然不幸的是我在 DispatchQueue
上找不到任何可以证实这一点的文档。
然而,looking at the source code表明情况确实如此:
public convenience init(
label: String,
attributes: DispatchQueueAttributes = .serial,
target: DispatchQueue? = nil)
{
...
}
虽然我总是更喜欢显式指定属性,以使我的代码更清晰并防止这种混淆。
UPD Swift 5
@Hamish 的回答是正确的:默认情况下新的 DispatchQueue 是串行的。
尽管源代码中属性的默认值不再像以前那样指定“.serial”:
public convenience init(
label: String,
qos: DispatchQoS = .unspecified,
attributes: Attributes = [],
autoreleaseFrequency: AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil)
值得注意的是,属性值集有 .concurrent
选项,但不再有 .serial
选项。
同样,默认情况下为串行,如果在属性数组中明确指定,则为并发。
这就是问题,直截了当。
let serial = DispatchQueue(label: "serial", attributes: .serial)
let concurrent = DispatchQueue(label: "concurrent", attributes: .concurrent)
let q = DispatchQueue(label: "q")
我看不到 属性 我可以检查 q
会告诉我。
运行 in playground with PlaygroundPage.current.needsIndefiniteExecution = true
显示串行行为,但我不想依赖 playground(有点像异步东西)或未记录的行为。
任何人都可以提供 link 文档的硬性答案吗?
在 Swift 3 之前,默认的调度队列类型是串行的——passing nil
into the attributes parameter of dispatch_queue_create
会产生一个串行队列,我看不出默认队列类型是改变。虽然不幸的是我在 DispatchQueue
上找不到任何可以证实这一点的文档。
然而,looking at the source code表明情况确实如此:
public convenience init(
label: String,
attributes: DispatchQueueAttributes = .serial,
target: DispatchQueue? = nil)
{
...
}
虽然我总是更喜欢显式指定属性,以使我的代码更清晰并防止这种混淆。
UPD Swift 5
@Hamish 的回答是正确的:默认情况下新的 DispatchQueue 是串行的。 尽管源代码中属性的默认值不再像以前那样指定“.serial”:
public convenience init(
label: String,
qos: DispatchQoS = .unspecified,
attributes: Attributes = [],
autoreleaseFrequency: AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil)
值得注意的是,属性值集有 .concurrent
选项,但不再有 .serial
选项。
同样,默认情况下为串行,如果在属性数组中明确指定,则为并发。