By default, is DispatchQueue .serial or .concurrent?
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.
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.