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?
The main dispatch queue is a globally available serial queue executing tasks on the application's main thread. As the main thread is used for UI updates it's important to be conscious when executing tasks on this queue.
The main queue in your app is serial. All the global pre-defined queues are concurrent. Any private dispatch queue you create is serial by default but can be set to be concurrent using an optional attribute as discussed in part 1.
DispatchQueue. main (the main queue) is a serial queue. sync and async do not determine serialization or currency of a queue, but instead refer to how the task is handled. Synchronous function returns the control on the current queue only after task is finished.
It works for concurrent queues, they default to the global concurrent queue with QoS default .
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With