iOS Development Multithreading CHAPTER-GCD Introduction

Source: Internet
Author: User
Tags gcd
<span id="Label3"></p><p><p><strong>iOS Development Multithreading CHAPTER-GCD Introduction</strong></p></p><p><p><strong>first, Brief Introduction</strong></p></p><p><p>1. What is gcd?</p></p><p><p>The full name is Grand Central Dispatch, which translates as "the backbone scheduler"</p></p><p><p>Pure C language, provides a lot of powerful functions</p></p><p><p></p></p><p><p>Advantages of 2.GCD</p></p><p><p>GCD is Apple's solution for multi-core parallel computing</p></p><p><p>GCD will automatically take advantage of more CPU cores (such as dual-core, quad-core)</p></p><p><p>GCD automatically manages the life cycle of threads (create threads, schedule tasks, Destroy Threads)</p></p><p><p>Programmers just need to tell gcd what tasks they want to perform, without having to write any thread management code</p></p><p><p></p></p><p><p>3. Tips</p></p>(1) GCD exists in Libdispatch.dylib this library, this library contains all the gcd, but any iOS program, the default load of the library, in the process of running the program will dynamically load the library, do not need to manually import. Click the +a button to import the Frame. (2) GCD is a pure C language, so when we write GCD related code, we face the function, not the Method. (3) Most of the functions in GCD start with Dispatch.<p><p><strong>Ii. Tasks and queues</strong></p></p><p><p>There are 2 core concepts in GCD</p></p><p><p>(1) Task: what action to perform</p></p><p><p>(2) queue: for storing tasks</p></p><p><p></p></p><p><p>The use of GCD is a 2-step process</p></p><p><p>(1) Custom Tasks</p></p><p><p>(2) determine what you want to do</p></p><p><p></p></p><p><p>Adding tasks to the queue, gcd automatically takes the tasks from the queue and puts them in the corresponding thread</p></p><p><p>Tip: the removal of a task follows the FIFO principle of the queue: first-in, last-out</p></p><p><p></p></p><p><p><strong>III. implementation of the mandate</strong></p></p><p><p>There are 2 functions in the 1.GCD to perform a task</p></p><p><p>Note: the right parameter (task) is submitted to the left parameter (queue) for Execution.</p></p><p><p>(1) perform the task Dispatch_sync (dispatch_queue_t queue, dispatch_block_t Block) in a synchronous manner;</p></p><p><p>Parameter description:</p></p><p><p>queue: queues</p></p><p><p>Block: Task</p></p><p><p></p></p><p><p>(2) Execute the task Dispatch_async asynchronously (dispatch_queue_t queue, dispatch_block_t block);</p></p><p><p>2. The difference between synchronous and asynchronous</p></p><p><p>Synchronization: executing in the current thread</p></p><p><p>Async: executing in another thread</p></p><p><p></p></p><p><p><strong>Iv. queues</strong></p></p><p><p>1. Types of queues</p></p><p><p>The GCD queue can be divided into 2 major types</p></p><p><p>(1) concurrent queues (Concurrent Dispatch Queue)</p></p><p><p>Multiple tasks can be executed concurrently (simultaneously) (automatically opening multiple threads simultaneously) concurrency features are only valid under asynchronous (dispatch_async) functions</p></p><p><p></p></p><p><p>(2) serial queuing (Serial Dispatch Queue)</p></p><p><p>Let the task execute one after the other (once a task has finished executing, then the next task is Performed)</p></p><p><p></p></p><p><p>2. Additional Information</p></p><p><p>There are 4 terms that are more easily confused: synchronous, asynchronous, concurrent, serial</p></p><p><p>Synchronous and asynchronous determine whether to open a new thread</p></p><p><p>Synchronous: performs a task in the current thread without the ability to open a new thread</p></p><p><p>Async: perform a task in a new thread with the ability to open a new thread</p></p><p><p></p></p><p><p>Concurrency and serialization determine how tasks are executed</p></p><p><p>Concurrency: multiple tasks concurrently (concurrently) executed</p></p><p><p>Serial: once a task is completed, the next task is performed</p></p><p><p></p></p><p><p>3. Serial Queue</p></p><p><p>There are 2 ways to get serial in GCD</p></p><p><p>(1) creating a serial queue using the Dispatch_queue_create function</p></p><p><p>dispatch_queue_t dispatch_queue_create (const Char *label, dispatch_queue_attr_t attr); Queue name, Queue property, general null</p></p><p><p>Example:</p></p><p><p>dispatch_queue_t queue = Dispatch_queue_create ("wendingding", NULL); Create</p></p><p><p>Dispatch_release (queue); Non-arc needs to release manually created queues</p></p><p><p></p></p><p><p>(2) using the primary queue (the queue associated with the mainline Threads)</p></p><p><p>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</p></p><p><p>Use Dispatch_get_main_queue () to get the home row</p></p><p><p>Example:</p></p><p><p>dispatch_queue_t queue = Dispatch_get_main_queue ();</p></p><p><p></p></p><p><p>4. Concurrent Queues</p></p><p><p>The GCD default already provides a global concurrency queue for the entire app to use, without the need to manually create</p></p><p><p>Using the Dispatch_get_global_queue function to obtain a global concurrency queue</p></p><p><p>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</p></p><p><p>Example:</p></p>This parameter is reserved for later use, temporarily not used, pass a 0. The first parameter is the priority, where the default is Selected. Gets a global default priority for concurrent Queues.<p><p>dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_default, 0); Get global concurrency Queue</p></p><p><p></p></p><p><p></p></p><p><p>Description: priority for global concurrent queues</p></p><p><p>#define Dispatch_queue_priority_high 2//high</p></p><p><p>#define Dispatch_queue_priority_default 0//default (medium)</p></p><p><p>#define Dispatch_queue_priority_low (-2)//low</p></p><p><p>#define Dispatch_queue_priority_background Int16_min//backstage</p></p><p><p>5. Execution effect of various queues</p></p><p><p></p></p><p><p>five, code Example</p></p><p><p>(1) Adding a task to the concurrent queue with an asynchronous function</p></p><p><p></p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 08-gcd Basic use 4//5// Created by Apple on 14-6-24.6// Copyright (c) 201 4 years Itcase. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16-(void) viewDidLoad18 { [super viewdidload];20 //1. get global concurrency queue dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_default, 0); //2. Add tasks to the queue to perform tasks Async functions: Ability to open new threads dispatch_async (queue, ^{25 NSLog (@ "download image 1----%@", [nsthread currentthread]) ; dispatch_async (queue, ^{28 NSLog (@ "download image 2----%@", [nsthread currentthread]); Dispatch_async (queue, ^{31 NSLog (@ "download image 2----%@", [nsthread currentthread]); 33//print main thread NSLog (@ "main thread----%@", [nsthread mainthread]); }37 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Summary: Open three sub-threads at the same time</p></p><p><p>(2) adding tasks to the serial queue with an asynchronous function</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre> 1//2//YYVIEWCONTROLLER.M 3//09-GCD Basic use 2 4//5//Created by Apple on 14-6-24.6//Copyright (c) 2014 it Case. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontroller (void) viewDidLoad18 {[super viewdidload];20 21///print main thread: NSLog (@ "main thread----%@", [nsthread Mainthrea d]); 23 24//create Serial queue dispatch_queue_t queue= dispatch_queue_create ("wendingding", NULL); 26//the first parameter is the name of a serial queue called, is the C language of the string 27//the second parameter is a queue of properties, generally speaking the serial queue does not need to assign any properties, so usually null value (null) 28 29//2. Add a task to the queue to execute the Dispatch_async (queue, ^{3 1 NSLog (@ "download picture 1----%@", [nsthread currentthread]); dispatch_async (queue, ^{34 NSLog (@ "download Image 2 ----%@ ", [nsthread currentthread]); dispatch_async (queue, ^{37 NSLog (@" download picture 2----%@ ", [nsthread Curre ntthread]); 38}); 39 40//3. release Resources//dispatch_release (queue),}43 @end </pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Summary: threads are turned on, but only one thread is opened</p></p><p><p>(3) Adding a task to the concurrent queue with a synchronous function</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 10-CGD Basic use 3 4//5// Created by Apple on 14-6-24.6// Copyright (c) 20 14 Itcase. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol LER16/**17 * Add a task to a concurrent queue with a synchronization function. */19-(void) viewDidLoad20 { [super viewdidload];22] /print main thread NSLog (@ "main thread----%@", [nsthread mainthread]); +/ /create serial queue dispatch_queue_t queue= dispatch_get_global_queue (dispatch_queue_priority_default, 0); Add a task to the queue to execute Dispatch_sync (queue, ^{32 NSLog (@ "download picture 1----%@", [nsthread currentthread]); Dispatch_sync ( queue, ^{35 NSLog (@ "download picture 2----%@", [nsthread currentthread]); notoginseng dispatch_sync (queue, ^{38 NSLog (@ "download picture 3----%@", [nsthread currentthread]); }41 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Summary: new threads are not opened and concurrent queues lose concurrency</p></p><p><p>(4) Adding a task to the serial queue with a synchronous function</p></p><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><pre><pre>1//2// yyviewcontroller.m 3// 11-CGD Basic use 4 4//5// Created by Apple on 14-6-24.6// Copyright (c) 20 14 Itcase. All Rights Reserved. 7//8 9 #import "YYViewController.h" @interface yyviewcontroller () @end14 @implementation Yyviewcontrol Ler16/**19 * Add a task to the serial queue with a synchronous Function. */21-(void) viewDidLoad22 { [super viewdidload];24 NSLog (@) Add a task to the serial queue with a synchronous function ");/ /print main thread NSLog (@" main thread----%@ ", [nsthread mainthread]);/ /create serial Queue 29 dispatch_queue_t queue= dispatch_queue_create ("wendingding", NULL); Add task to queue execution 32 dispatch_sync (queue, ^{33 NSLog (@ "download picture 1----%@", [nsthread currentthread]); Dispatch_ Sync (queue, ^{36 NSLog (@ "download image 2----%@", [nsthread currentthread]); dispatch_sync (queue, ^{39 NSLog (@ "download picture 3----%@", [nsthread currentthread]); }42 @end</pre></pre><span class="cnblogs_code_copy"><span class="cnblogs_code_copy"></span></span><p><p></p></p><p><p>Summary: new threads are not opened</p></p><p><p>(5) Supplement</p></p><p><p>Add: the role of the queue name:</p></p><p><p>When you debug in the future, you can see which queue the task is executing IN.</p></p><p><p></p></p><p><p>(6) Summary</p></p><p><p>Description: The synchronization function does not have the ability to open threads, no matter what queue does not open the thread, the asynchronous function has the ability to open threads, open a few threads by the queue (the serial queue will only open a new thread, the concurrent queue will open multiple threads).</p></p><p><p>Synchronization functions</p></p><p><p>(1) Concurrent Queue: No thread</p></p><p><p>(2) Serial Queue: No thread</p></p><p><p>Asynchronous functions</p></p><p><p>(1) Concurrent queue: can open n threads</p></p><p><p>(2) serial queue: Open 1 threads</p></p><p><p>Add:</p></p>In all functions, the names of functions, such as create\copy\new\retain, need to be release when the data is not Needed. The GCD data type does not need to be release in the Arc Environment. The data type of the CF (core Foundation) is still required for release in the Arc Environment. Asynchronous functions have the ability to thread, but not necessarily the thread<p><p>iOS Development Multithreading CHAPTER-GCD Introduction</p></p></span>

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.