[SWIFT] [GCD] Basics of DispatchQueue class

DispatchQueue class

An object that manages the serial or parallel execution of tasks in the main or background thread of your app.


class DispatchQueue : DispatchObject

Dispatch queues are FIFO (first in, first out) queues that allow applications to pass tasks in the form of block objects. Dispatch queues perform tasks serially or in parallel. Tasks placed on the dispatch queue are executed in "any thread" managed by the system. ** The system does not guarantee "in which thread the task will be executed", except for the dispatch queue, which is the main thread of the app. ** **

Tasks are scheduled synchronously or asynchronously. When you schedule a task synchronously, the code waits for the next task until the item finishes executing. When you schedule work items asynchronously, other tasks continue to run while they are running elsewhere.

** A deadlock occurs when trying to perform work synchronously on the main queue. ** **

Don't create extra threads

When designing tasks that run in parallel, avoid calling "methods that block running threads." When a "task scheduled by a parallel dispatch queue" blocks that thread, the system creates an additional thread to run "other parallel tasks in the queue." If too many tasks are blocked, the system can run out of threads in the app.

Creating a "private parallel dispatch queue" can also drain your app's threads. Each dispatch queue consumes thread resources, so creating additional parallel dispatch queues will consume more and more threads. Instead of creating a private parallel queue, submit the task to the "Parallel Global Dispatch Queue". For serial tasks, set the serial queue target to one of the "simultaneous global queues". By doing so, you can maintain the behavior of serial queues while minimizing "queues that create separate threads".


main property

The dispatch queue associated with the main thread of the current process.


class var main: DispatchQueue { get }

The system associates the automatically created main queue with the application's main thread. The app performs a "block sent to the main queue" in one of the following approaches.

--Call the dispatchMain () method --Call the UIApplicationMain (_: _: _: _ :) method on iOS and the NSApplicationMain (_: _ :) method on macOS to start the app. --Use the main thread CFRunLoop

Calling the suspend (), resume (), and dispatch_set_context (_: _ :) methods on the queue in this property has no effect on the queue.

If the main thread does not respond for too long, you may get a 0x8badf00d exception on the main thread of mach_msg_trap. On iOS, this exception can occur if an app detects that it has failed to respond to a particular UI event in time. There is a guard dog on iOS that monitors the responsiveness of the UI.

If your app has tasks that take a long time to perform, such as network connectivity, run them in the system's global queue or another background dispatch queue. If possible, use asynchronous calls.


global (qos :) method

Returns a "system global queue" with a specified service quality level.


class func global(qos: DispatchQoS.QoSClass = .default) -> DispatchQueue

This method returns the best queue to perform the task at the specified level of service quality. Calling the suspend (), resume (), and dispatch_set_context (_: _ :) functions does not affect the returned queue. Tasks submitted to the returned queue are scheduled in parallel relative to each other.

An example of creating a background queue with a standard service quality level

let backgroundQueue = DispatchQueue.global(qos: .default)


init(label:qos:attributes:autoreleaseFrequency:target:) Create a new dispatch queue that can send "blocks of code".


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

The parameters are explained below.

--. Inherit (inherits the auto-release frequency of the target queue) --. WorkItem (released after the block finishes executing) --. Never (The block will not be auto-released even after execution.)

Example of creating a private background queue

let videoDataOutputQueue = DispatchQueue(label: "VideoDataOutput", 
                                           qos: .userInitiated, 
                                    attributes: [], 
                          autoreleaseFrequency: .workItem)

Recommended Posts

[GCD] Basics of DispatchQueue class
[GCD] Basics of parallel programming in Swift
Basics of Ruby
[Ruby] Summary of class definitions. Master the basics.
Use of Date class
Basics of try-with-resources statement
[Ruby] Class nesting, inheritance, and the basics of self
[Rails] Introduction of devise Basics
Docker monitoring-explaining the basics of basics-
Basics of character operation (java)
Understand the basics of docker
Example of using abstract class
[Java] Comparator of Collection class
Trap of asList of Arrays class
The basics of Swift's TableView
Summary of Java language basics
Summary of Java Math class
[Java basics] What is Class?
Basics of conditional branching and return
About the basics of Android development
Basics of sending Gmail in Ruby
Various methods of Java String class
The basics of SpringBoot + MyBatis + MySQL
[Ruby] Date, Time, Datetime class basics
Various methods of the String class
[Spring Boot] Role of each class
Basics of Ruby ~ Review of confusing parts ~
Ruby Basics 2 ~ Review of confusing parts ~