iOS multithreaded operation (iv)--GCD core concept

Source: Internet
Author: User


GCD full name Grand Central Dispatch, which can be translated as "large dispatch hub Scheduler", written in pure C, provides many very powerful functions. GCD is an Apple solution for multi-core parallel computing that automatically uses more CPU cores to participate in operations, automatically manages thread life weeks (creating threads, scheduling tasks, destroying threads), and programmers just need to tell gcd what they want to do, No need to write any thread management code! there are two core concepts in GCD, one is task, the other is queue. Task: What action to perform. Tasks are pre-packaged in block to prepare a piece of code to execute. queue: Used to store tasks, according to the first in-out way, the scheduling task on which thread to execute. there are two functions in gcd that are used to perform tasks, namely, synchronous functions and asynchronous functions. synchronization function: The code form of the function is as followsDispatch_sync (dispatch_queue_t queue, ^ (void) block)where queue is queued, block for taskThe synchronization task does not create a thread, but it executes in the current thread (which can be a child thread, the page can be the main thread), and the synchronization task has an attribute, as long as the synchronization task is added to the queue to be executed immediately, the synchronization task will not execute the subsequent code. For example:
-(void) viewdidload {    [super viewdidload];    Get global queue    dispatch_queue_t q = dispatch_get_global_queue (0, 0);    Perform the synchronization task    Dispatch_sync (q, ^{        //Let the thread performing the task hibernate 2s        [nsthread sleepfortimeinterval:2.0];        NSLog (@ "block-%@", [Nsthread CurrentThread]);    NSLog (@ "viewdidload-%@", [Nsthread CurrentThread]);}

The results of the operation are as follows:

the characteristics of the synchronization task can be verified accordingly
Asynchronous functions:Dispatch_async (dispatch_queue_t queue, ^ (void) block)in general, an asynchronous task opens a thread that executes in the background (except in one case, as described later), and one of the characteristics of an asynchronous task is to execute 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);    Executes the asynchronous task    Dispatch_async (q, ^{        //For        (int i = 0; i<10; i++) {            NSLog (@ "block-%@-%d", [Nsthread C urrentthread],i);        }    );    NSLog (@ "viewdidload-%@", [Nsthread CurrentThread]);}

The results of the operation are as follows:

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

The results of the implementation are as follows:
This allows the queue to open as many bars as we cannot control, and the async function does not wait for the current thread (the current thread is the main path) to work.

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.