Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 728
SimplGy Avatar asked Jun 29 '16 22:06

SimplGy


People also ask

Is DispatchQueue global serial or concurrent?

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.

Is Main queue serial or concurrent?

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.

Is DispatchQueue main serial?

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.

What is the default queue in Swift?

It works for concurrent queues, they default to the global concurrent queue with QoS default .


2 Answers

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.

like image 53
rommex Avatar answered Sep 19 '22 14:09

rommex


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.

like image 26
Hamish Avatar answered Sep 17 '22 14:09

Hamish