Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In GCD, are all tasks in a serial queue guaranteed to run in the same thread?

In GCD, are all tasks in a serial queue (e.g. main queue) guaranteed to run in the same thread?

like image 919
Boon Avatar asked Oct 10 '13 15:10

Boon


People also ask

Is global queue serial or concurrent?

Concurrent queues (also known as a type of global dispatch queue) execute one or more tasks concurrently, but tasks are still started in the order in which they were added to the queue. The currently executing tasks run on distinct threads that are managed by the dispatch queue.

Is DispatchQueue main concurrent?

In general, dispatch queues will only perform one task at a time in a first-in, first-out kind of fashion. They can, however, be configured to schedule work concurrently. The main dispatch queue is a queue that runs one task at a time.

What is serial and concurrent queue in Swift?

CONCURRENT QUEUES (often known as global dispatch queues) can execute tasks simultaneously; the tasks are, however, guaranteed to initiate in the order that they were added to that specific queue, but unlike serial queues, the queue does not wait for the first task to finish before starting the second task.


1 Answers

For serial queues generally, no. From the Concurrency Programming Guide:

Serial queues (also known as private dispatch queues) execute one task at a time in the order in which they are added to the queue. The currently executing task runs on a distinct thread (which can vary from task to task) that is managed by the dispatch queue.

For the main queue specifically, yes:

The main dispatch queue is a globally available serial queue that executes tasks on the application’s main thread. […] Because it runs on your application’s main thread, the main queue is often used as a key synchronization point for an application.

like image 160
Aaron Brager Avatar answered Oct 22 '22 04:10

Aaron Brager