Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between the "global queue" and the "main queue" in GCD?

Among some other ways, there are these two ways to get queues in GCD:

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);  dispatch_get_main_queue(); 

If I'm not completely wrong, the "main queue" is executing on the main thread and is good for "callback" blocks which execute UI work.

Does this mean a "global queue" is one that runs on a background thread?

like image 260
Proud Member Avatar asked Mar 07 '12 13:03

Proud Member


People also ask

What is the main queue?

The main queue is the dispatch queue in which all the UI updates take place and the code involving UI changes are placed. There are two types of main queue calls synchronous and asynchronous .

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.

How many types of dispatch queues are there?

There are two types of dispatch queues, serial dispatch queues and concurrent dispatch queues.

What is global thread in Swift?

sync means doing thing in main thread/UI thread and x. async means doing in background thread. Global means performing something with concurrent queue i.e Parallel task.


1 Answers

The main queue does indeed run on the main thread like you say.

The global queues are concurrent queues and from the main page for dispatch_get_global_queue:

Unlike the main queue or queues allocated with dispatch_queue_create(), the global concurrent queues schedule blocks as soon as threads become available ("non-FIFO" completion order). The global concurrent queues represent three priority bands:

       •   DISPATCH_QUEUE_PRIORITY_HIGH        •   DISPATCH_QUEUE_PRIORITY_DEFAULT        •   DISPATCH_QUEUE_PRIORITY_LOW 

Blocks submitted to the high priority global queue will be invoked before those submitted to the default or low priority global queues. Blocks submitted to the low priority global queue will only be invoked if no blocks are pending on the default or high priority queues.

So, they are queues which run on background threads as and when they become available. They're "non-FIFO" so ordering is not guaranteed.

like image 114
mattjgalloway Avatar answered Oct 20 '22 14:10

mattjgalloway