What is the difference between a “global queue” and a “main queue” in a GCD?

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

 dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_get_main_queue(); 

If I'm not completely mistaken, the "main queue" is executed in the main thread and is good for callback blocks that do the work of the user interface.

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

+56
ios concurrency iphone grand-central-dispatch
Mar 07 2018-12-12T00:
source share
5 answers

The main queue really works on the main thread, as you say.

Global queues are parallel queues and from the main page for dispatch_get_global_queue:

Unlike the main queue or the queues allocated by dispatch_queue_create (), global parallel queues schedule blocks as soon as threads become available (non-FIFO termination order). Global parallel queues represent three priority bands:

  • DISPATCH_QUEUE_PRIORITY_HIGH • DISPATCH_QUEUE_PRIORITY_DEFAULT • DISPATCH_QUEUE_PRIORITY_LOW 

Blocks sent to the high priority global queue will be called before those presented in the global default or low priority queues. Blocks sent to the low priority global queue will be called if no blocks are expected in the default or high priority queue.

Thus, they are queues that operate on background threads as they appear. They are "non-FIFO", so ordering is not guaranteed.

+77
Mar 07 2018-12-12T00:
source share

5 queues (4 backgrounds, 1 main) have different thread priorities ( -[NSThread threadPriority] ):

  -main- : 0.758065 DISPATCH_QUEUE_PRIORITY_HIGH : 0.532258 DISPATCH_QUEUE_PRIORITY_DEFAULT : 0.500000 DISPATCH_QUEUE_PRIORITY_LOW : 0.467742 DISPATCH_QUEUE_PRIORITY_BACKGROUND : 0.000000 

(tested on 4th generation iPod and MacBook Pro simulator)

+27
Jan 30 '14 at 12:12
source share

Yes. You can run such code on the device to check it:

 dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSLog(@"Block 1a"); NSAssert(![NSThread isMainThread], @"Wrong thread!"); NSLog(@"Block 1b"); }); dispatch_async( dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"Block 2a"); NSAssert([NSThread isMainThread], @"Wrong thread!"); NSLog(@"Block 2b"); }); }); 
+7
Mar 07 2018-12-12T00:
source share

GCD provides three main types of queues:

  1. Main queue : runs in the main thread and is a sequential queue. This is a common choice for updating the user interface after completing a task in a parallel queue.

  2. Global queues : concurrent queues common to the entire system. This is a common choice for doing work without a user interface in the background. There are four such queues with different priorities: high , default , low and background . The priority priority queue has the lowest priority and is adjustable in any I / O operation to minimize the negative impact on the system. When configuring global concurrent queues, priority is not specified directly. Instead, you specify a property of the Quality of Service (QoS) class:

    but. User-interactive - This presents tasks that must be completed immediately to provide a pleasant user experience. This should be done in the main thread .

    b. User-initiated - represents tasks that are initiated from the user interface and can be performed asynchronously. It should be used when the user expects immediate results and tasks necessary to continue interacting with the user.

    from. Utility - presents long-term tasks, usually with a progress indicator visible to the user. Use it for computing , input / output , networking , continuous data feed and similar tasks. This class is designed to save energy.

    e. Background - represents tasks that the user does not know directly. Use it for prefetching, maintenance, and other tasks that do not require user interaction and are not time dependent.

  3. Custom queues : requests in these queues actually fall into one of the global queues. Queues you created that might be

    but. Serial Queue - The only global sequential queue is DispatchQueue.main, but you can create a private serial queue.

    b. Concurrent Queue - A good choice when you want to do background work sequentially and track it.

Synchronous and asynchronous

More here , here

+5
Mar 07 '19 at 12:57
source share

Global dispatch queue:

  • Tasks in a parallel queue run simultaneously [background threads]
  • Tasks are still running in the order in which they were added to the queue.

Main dispatch queue:

  • An available sequential queue that performs tasks on the main thread application.
  • It is usually called from the background queue when some background processing is completed, and the user interface should be updated.
+4
Jun 01 '17 at 6:48
source share



All Articles