In GCD, are all tasks in a serial queue (e.g. main queue) guaranteed to run in the same thread?
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.
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.
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.
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.
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