14

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?

SimplGy
  • 20,079
  • 15
  • 107
  • 144

2 Answers2

18

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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
12

UPD Swift 5

The answer by @Hamish holds true: a new DispatchQueue is serial by default. And though the default value for attributes in the source code does not specify ".serial" anymore as it used to:

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

it is noteworthy that the set of Attribute values has .concurrent option but not .serial any more. So again, serial by default, and concurrent if otherwise explicitly assigned in the attributes array.

rommex
  • 763
  • 1
  • 8
  • 21