Search code examples
cocoagrand-central-dispatch

By default, is DispatchQueue .serial or .concurrent?


That's the question, straight-forward as that.

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

I see no property I can inspect on q that will tell me.

Running in playground with PlaygroundPage.current.needsIndefiniteExecution = true shows serial behavior, but I don't want to rely on playground (kind of janky with async stuff), or undocumented behavior.

Can anyone offer a hard answer with a link to documentation?


Solution

  • Prior to Swift 3, the default dispatch queue type was serial – passing nil into the attributes parameter of dispatch_queue_create would yield a serial queue, and I see no reason for the default queue type to change. Although unfortunately I can't find any documentation on DispatchQueue that can confirm this.

    However, looking at the source code reveals that this is indeed still the case:

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

    Although I always prefer to specify the attribute explicitly, to make my code clearer and prevent this kind of confusion.