iOS multithreaded operation (iv)--GCD core concept

Source: Internet
Author: User


GCD full name Grand Central Dispatch. It can be translated as "large dispatch Central scheduler", which is written in pure C language and provides many powerful functions. GCD is the solution that Apple has proposed for multi-core parallel operations, and it can actively use many other CPU cores to participate in the operation. Proactively manages the thread's life cycle (creating threads, scheduling tasks, destroying threads). and the program ape just needs to tell gcd what task he wants to run, no need to write whatever thread manages the code!

there are two core concepts in GCD, one is the task. The second is the queue. Task: What action to run. Tasks are pre-packaged in block to prepare a piece of code to run. queue: Used to store tasks, according to the first-in-one-out way, the scheduling task on which thread to run. There are two functions in gcd that are used to run tasks, each of which is a synchronous function and an asynchronous function.

synchronization function: The code form of the function is as followsDispatch_sync (dispatch_queue_t queue, ^ (void) block)in which queue is queued, block is the taskThe synchronization task does not create a thread, but it runs in the current thread (which can be a child thread and the page can be the main thread). A synchronization task has a feature. Just synchronize the tasks as soon as they are added to the queue to run immediately. The synchronization task does not run until the next code is run.

For example:
-(void) viewdidload {    [super viewdidload];    Get global queue    dispatch_queue_t q = dispatch_get_global_queue (0, 0);    Run the Sync task    dispatch_sync (q, ^{        //Let the thread running the task hibernate 2s        [nsthread sleepfortimeinterval:2.0];        NSLog (@ "block-%@", [Nsthread CurrentThread]);    NSLog (@ "viewdidload-%@", [Nsthread CurrentThread]);}

the results of the execution are as follows:

the ability to verify the characteristics of the synchronization task
Asynchronous functions:Dispatch_async (dispatch_queue_t queue, ^ (void) block)in general, asynchronous tasks open a thread that runs in the background, except in one case. Later on), one of the features of an asynchronous task is to run directly without waiting for the task of the current thread.For example:
-(void) viewdidload {    [super viewdidload];    Get global queue    dispatch_queue_t q = dispatch_get_global_queue (0, 0);    Run asynchronous task    Dispatch_async (q, ^{        //For        (int i = 0; i<10; i++) {            NSLog (@ "block-%@-%d", [Nsthread Curre ntthread],i);        }    );    NSLog (@ "viewdidload-%@", [Nsthread CurrentThread]);}

the results of the execution are as follows:

second execution Result:
You can see that two execution results are different and not in the same thread.Suppose you change the code to such as the following form:
-(void) viewdidload {    [superviewdidload];    Get global queue    dispatch_queue_t Q =dispatch_get_global_queue (0, 0);    Run the asynchronous task for    (int i = 0; i<; i++) {        dispatch_async (q, ^{            NSLog (@ "block-%@-%d", [Nsthreadcurrentthread ],i);}        );            }    NSLog (@ "viewdidload-%@", [Nsthreadcurrentthread]);}

running results such as the following:
This allows the queue to open how many bars we have no control over. The async function does not wait for the current thread (the current thread is the main path) for the task.

iOS multithreaded operation (iv)--GCD core concept

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.