iOS Development Multithreaded Article-GCD Introduction-Top of the text

Source: Internet
Author: User

Original http://www.cnblogs.com/wendingding/p/3806821.html

iOS Development Multithreading Chapter-GCD Introduction

First, Brief introduction

1. What is GCD?

The full name is Grand Central Dispatch, which translates as "the backbone scheduler"

Pure C language, provides a lot of powerful functions

Advantages of 2.GCD

GCD is Apple's solution for multi-core parallel computing

GCD will automatically take advantage of more CPU cores (such as dual-core, quad-core)

GCD automatically manages the life cycle of threads (create threads, schedule tasks, destroy threads)

Programmers just need to tell gcd what tasks they want to perform, without having to write any thread management code

Ii. Tasks and queues

There are 2 core concepts in GCD

(1) Task: what action to perform

(2) Queue: for storing tasks

The use of GCD is a 2-step process

(1) Custom tasks

(2) Determine what you want to do

Adding tasks to the queue, GCD automatically takes the tasks from the queue and puts them in the corresponding thread

Tip: The removal of a task follows the FIFO principle of the queue: first-in, last-out

III. implementation of the mandate

There are 2 functions in the 1.GCD to perform a task

(1) Perform the task Dispatch_sync (dispatch_queue_t queue, dispatch_block_t block) in a synchronous manner;

Parameter description:

Queue: Queues

Block: Task

(2) Execute the task Dispatch_async asynchronously (dispatch_queue_t queue, dispatch_block_t block);

2. The difference between synchronous and asynchronous

Synchronization: Executing in the current thread

Async: Executing in another thread

Iv. queues

1. Types of queues

The GCD queue can be divided into 2 major types

(1) Concurrent queues (Concurrent Dispatch queue)

Multiple tasks can be executed concurrently (simultaneously) (automatically opening multiple threads simultaneously) concurrency features are only valid under asynchronous (Dispatch_async) functions

(2) Serial queuing (Serial Dispatch queue)

Let the task execute one after the other (once a task has finished executing, then the next task is performed)

2. Additional Information

There are 4 terms that are more easily confused: synchronous, asynchronous, concurrent, serial

Synchronous and asynchronous determine whether to open a new thread

Synchronous: Performs a task in the current thread without the ability to open a new thread

Async: Perform a task in a new thread with the ability to open a new thread

Concurrency and serialization determine how tasks are executed

Concurrency: Multiple tasks concurrently (concurrently) executed

Serial: Once a task is completed, the next task is performed

3. Serial Queue

There are 2 ways to get serial in GCD

(1) Creating a serial queue using the Dispatch_queue_create function

dispatch_queue_t dispatch_queue_create (const char *label, dispatch_queue_attr_t attr); Queue name, queue property, general null

Example:

dispatch_queue_t queue = dispatch_queue_create ("wendingding", NULL); Create

Dispatch_release (queue); Non-Arc needs to release manually created queues

(2) using the primary queue (the queue associated with the mainline threads)

The primary queue is a special serial queue that comes with the GCD, and the tasks placed in the master queue are placed in the main thread to execute

Use Dispatch_get_main_queue () to get the home row

Example:

dispatch_queue_t queue = Dispatch_get_main_queue ();

4. Concurrent queues

The GCD default already provides a global concurrency queue for the entire app to use, without the need to manually create

Using the Dispatch_get_global_queue function to obtain a global concurrency queue

dispatch_queue_t Dispatch_get_global_queue (dispatch_queue_priority_t priority,unsigned long flags); This parameter is temporarily useless and can be used with 0

Example:

dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); Get global concurrency Queue

Description: Priority for global concurrent queues

#define DISPATCH_QUEUE_PRIORITY_HIGH 2//High

#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0//default (Medium)

#define Dispatch_queue_priority_low (-2)//Low

#define Dispatch_queue_priority_background int16_min//Backstage

5. Execution effect of various queues

Five, code example

(1) Adding a task to the concurrent queue with an asynchronous function

 1//2//Yyviewcontroller.m3//08-GCD Basic Use4//5//Created by Apple on 14-6-24.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"1011 @Interface Yyviewcontroller ()12@end14@implementation Yyviewcontroller1617-(void) Viewdidload18 {[Super Viewdidload];20//1. Get a global concurrent queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);22//2. Add a task to the queue to perform the task23//Async functions: Ability to open new threadsDispatch_async (Queue, ^{NSLog (@ " download image 1----%@", [Nsthread CurrentThread]);  26}); dispatch_async (queue, ^{NSLog (@ " download image 2----%@", [Nsthread CurrentThread]);  29}); dispatch_async (queue, ^{NSLog (@ " download image 2----%@", [Nsthread CurrentThread]);  32});  // print main thread NSLog (@ "main thread ----%@", [Nsthread Mainthread]);   @end             

Summary: Open three sub-threads at the same time

(2) Adding tasks to the serial queue with an asynchronous function

 1//2//Yyviewcontroller.m3//09-GCD Basic Use 24//5//Created by Apple on 14-6-24.6//Copyright (c) 2014 itcase. All rights reserved.7//89#import"YYViewController.h"1011 @Interface Yyviewcontroller ()12@end14@implementation Yyviewcontroller1617-(void) Viewdidload18 {[Super Viewdidload];2021st//Print main threadNSLog (@"Main thread----%@ ", [Nsthread Mainthread]);2324//Create a serial queuedispatch_queue_t queue= Dispatch_queue_create ("Wendingding ", NULL);26//The first parameter is the name of the serial queue and is a C-language string27//The second parameter is the property of the queue, in general the serial queue does not need to assign any properties, so it is usually null (NULL)2829//2. Add a task to the queue to executeDispatch_async (Queue, ^{NSLog (@ " download picture 1----%@", [Nsthread CurrentThread]);  32}); dispatch_async (queue, ^{NSLog (@ " download image 2----%@", [Nsthread CurrentThread]);  35}); dispatch_async (queue, ^{Notoginseng NSLog (@ " download image 2----%@", [Nsthread CurrentThread]);  38});   3. Release resources  // dispatch_release (queue);   @end               

Summary: Threads are turned on, but only one thread is opened

(3) Adding a task to the concurrent queue with a synchronous function

 1//2//Yyviewcontroller.m3//10-CGD Basic Use 34//5//Created by Apple on 14-6-24.6//Copyright (c) 2014 itcase. All rights reserved.7//89 #Import"YYViewController.h "10@interface Yyviewcontroller ()12@end14@implementation Yyviewcontroller16/**17 * Adding a task to a concurrent queue with a synchronous function 18 */19-(void) Viewdidload20 {21 [Super Viewdidload];2223//Print main threadNSLog (@"Main thread----%@ ", [Nsthread Mainthread]);2526//Create a serial queuedispatch_queue_t queue= Dispatch_get_global_queue (Dispatch_queue_priority_default,R);   2. Add task to queue execute dispatch_sync (queue, ^{NSLog (@" download picture 1----%@", [ Nsthread CurrentThread]); 33}); dispatch_sync (queue, ^{NSLog (@" Download image 2----%@", [Nsthread CurrentThread]);  36}); PNs Dispatch_sync (queue, ^{NSLog (@" Download image 3----%@", [Nsthread CurrentThread]);  39}); 40}  @end                 

Summary: New threads are not opened and concurrent queues lose concurrency

(4) Adding a task to the serial queue with a synchronous function

 1//2//Yyviewcontroller.m3//11-CGD Basic Use 44//5//Created by Apple on 14-6-24.6//Copyright (c) 2014 itcase. All rights reserved.7//87 mImport"YYViewController.h "10@interface Yyviewcontroller ()12@end14@implementation Yyviewcontroller161718/**19 * Add a task to the serial queue with a sync function 20 */21-(void) Viewdidload22 {23 [Super Viewdidload];NSLog (@"Add a task to the serial queue with a synchronous function ");25//Print main threadNSLog (@"Main thread----%@ ", [Nsthread Mainthread]);2728//Create a serial queuedispatch_queue_t queue= Dispatch_queue_create ("wendingding", NULL);   2. Add task to queue execute dispatch_sync (queue, ^{NSLog (@" download picture 1----%@", [Nsthread CurrentThread]); 34}); dispatch_sync (queue, ^{NSLog (@" Download image 2----%@", [Nsthread CurrentThread]);  37}); dispatch_sync (queue, ^{NSLog (@" download picture 3----%@", [Nsthread CurrentThread]);  40}); 41}  @end                 

Summary: New threads are not opened

(5) Supplement

Add: The role of the queue name:

When you debug in the future, you can see which queue the task is executing in.

iOS Development Multithreaded Article-GCD Introduction-Top of the text

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.