GCD global Queue and primary queue

Source: Internet
Author: User

The GCD default already provides a global concurrency queue for the entire app to use, so you don't have to create them manually.

The function that creates the global queue is
dispatch_queue_t q = dispatch_get_global_queue (long identifier, unsigned long flags)
The parameter type is: Long Identifier:ios 8.0 tells the queue to perform the "quality of service quality of services" for the task, and the parameters provided by the system are: qos_class_user_interactive 0x21, user interaction (hope to complete as soon as possible, users expect the results, do not put too much time-consuming operation) Qos_class_user_initiated 0x19, user expectations (do not put too much time-consuming action)
Qos_class_default 0x15, default (not for programmers, used to reset columns)
Qos_class_utility 0x11, utility (time consuming operation, you can use this option)
Qos_class_background 0x09, backstage
Qos_class_unspecified 0x00, unspecified
Pre-IOS 7.0 priority
Dispatch_queue_priority_high 2 High-priority
Dispatch_queue_priority_default 0 Default Priority
Dispatch_queue_priority_low (-2) low-priority
dispatch_queue_priority_background int16_min Background PriorityBackground indicates that the user does not need to know when the task is completed, if the choice of this option is very slow, very bad for debugging!         For priority recommendations do not take too much responsibility, it is easiest to avoid the priority reversal. unsigned long flags: The official Apple document explains this: flags that is reserved for the future use. tags are reserved for future use! So this parameter should always be specified as 0 if you do ios8.0 with ios7.0, you can create a global queue like this:
dispatch_queue_t q = dispatch_get_global_queue (0, 0);
Try to do the asynchronous operation with the global queue to see if it is executing concurrently, and the following code
dispatch_queue_t q = dispatch_get_global_queue (0, 0);    2. Asynchronously executes for    (int i = 0; i < ++i) {        dispatch_async (q, ^{            NSLog (@ "%@%d", [Nsthread CurrentThread], i);        });    }    NSLog (@ "come here");


Now we are to simulate an operation, the user needs to log in before performing two download operations, login and download is a time-consuming operation, so we need to open a thread in the background, according to the reality of two download operations need to execute concurrently, how to achieve this need? In this, we draw out the function of the synchronous functions! Have the following code
1. Queue    dispatch_queue_t q = dispatch_get_global_queue (0, 0);       2. Task    void (^task) = ^ {        dispatch_sync (q, ^{            NSLog (@ "Login%@", [Nsthread CurrentThread]);        });        Dispatch_async (q, ^{            NSLog (@ "Download A%@", [Nsthread CurrentThread]);        Dispatch_async (q, ^{            NSLog (@ "Download B%@", [Nsthread CurrentThread]);    };       3. Asynchronously executes    a task Dispatch_async (q, Task);


The results are as follows: One more time: The result is in any case on the child thread execution, and login is always in front, as to which thread first download we are not known, the results are simple to show, if you open more download basic book can show better results, here is not demonstrated.      In an MRC environment, the global queue does not need to free up memory.     Of course, the GCD itself comes with a special serial queue, and all tasks placed in the main queue are executed on the main thread. As soon as the program starts, the main thread is already there, and the home row is there, so the home column does not need to be created, just get it, as follows
dispatch_queue_t q = Dispatch_get_main_queue ();
Try to perform an asynchronous task in the main queue:
dispatch_queue_t q = Dispatch_get_main_queue ();       2. Asynchronously executes for    (int i = 0; i < ++i) {        dispatch_async (q, ^{            NSLog (@ "%@-%d", [Nsthread CurrentThread], i); 
   });    }    NSLog (@ "thread sleep 1s");    [Nsthread sleepfortimeinterval:1.0];    NSLog (@ "Come here-%@", [Nsthread CurrentThread]);
As a result, the main queue, even if there are asynchronous tasks, will be executed on the main thread, in this case, the code on the main thread is not finished, so the asynchronous task will wait until the task on the main thread executes. So what happens if I perform a synchronization task on the main thread? The following code:
1. Queue    DISPATCH_QUEUE_TQ = Dispatch_get_main_queue ();    NSLog (@ "Now I m Here");    2. Synchronous execution    Dispatch_sync (q, ^{        NSLog (@ "%@", [Nsthreadcurrentthread]);    });    NSLog (@ "come here");


The program only executes to the second line of code can not be executed, why this? The answer is that the main thread is blocked, that is, the deadlock!!!     If the screen has a UI that interacts with the user at this time, it will also lose interactivity, as if it were to crash!!! We should all know that the synchronization task has a feature, as long as the one added to the queue to execute immediately, the main queue forever as long as a thread-the main course, at this time the main thread waiting for the home row scheduling synchronization task, and the host column found that the main thread has not finished the task, will not let the synchronization task on the main thread This creates a wait for each other (the home queue waits for the main thread to finish executing the existing tasks, while the main thread waits for the host queue to schedule the synchronization task!).     ), this is called the deadlock!!! So what if there is a need to perform synchronization tasks in the main queue? We can use an asynchronous task to wrap a synchronization task into the main queue! As follows:
Global queue    dispatch_queue_t q = dispatch_get_global_queue (0, 0);    Task    Void (^task) () = ^ {        NSLog (@ "%@", [Nsthread CurrentThread]);       Perform synchronization tasks on the Home column        Dispatch_sync (Dispatch_get_main_queue (), ^{            NSLog (@ "Come here%@", [Nsthread CurrentThread]);        } );               NSLog (@ "hahaha%@", [Nsthread CurrentThread]);    };       Asynchronously executes    the task Dispatch_async (q, Task);    NSLog (@ "Now i m here-%@", [Nsthread CurrentThread]);


The execution results are as follows: The main thread is not blocked!!! The "Now I'm here" presence is indeterminate, but always precedes the sync function in the main queue!   The synchronization task for the home row is added to the global queue's asynchronous task, and the global queue first causes the task on the main thread to perform the synchronization task first! Original address: http://subscribe.mail.10086.cn/subscribe/readAll.do?columnId=563&itemId=3130310

GCD global Queue and primary queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.