iOS Development-multithreaded programming (GCD)

Source: Internet
Author: User
Tags gcd

About Grand Central Dispatch

The Grand Central Dispatch abbreviation GCD is a technology developed by Apple and a newer solution for multicore programming. It is primarily used to optimize applications to support multicore processors as well as other symmetric multi-processing systems.
GCD provides a very simple way to implement parallel processing. You can put the code you want to execute concurrently in a block clock, and then add the block to a queue.
There are 3 types of queues for the block we need to execute in GCD:

    • Main: This queue executes our block sequentially and ensures that the blocks are executed in the main thread.
    • Concurrent: This queue will follow the FIFO principle to execute blocks in it and automatically manage the threads for you.
    • Serial: This type of queue executes one block at a time and also follows the FIFO rules.
Basic use of GCD

Create a queue that will take more time-consuming work into the queue as a block to avoid blocking the main thread.

//创建一个队列dispatch_queue_t queue = dispatch_get_global_queue(00);    //将队列queue中的block以异步的形式执行    dispatch_async(queue, ^{        for (int0100; i++) {            [NSThread sleepForTimeInterval:0.02];        }    });

When creating a queue, you need to pass in two parameters. The first parameter determines the priority of the queue, and the higher the queue priority, the greater the number of blocks that are added to the team's joins are executed first. The parameters are selected in the following range:

#define DISPATCH_QUEUE_PRIORITY_HIGH 2#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0#define DISPATCH_QUEUE_PRIORITY_LOW (-2)#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN

The smaller the value, the lower the priority. A block in a high-priority queue is executed earlier than the block in the low-priority queue.

To make it easy to use GCD, Apple provides some ways for us to put blocks on the main thread or background thread, or to postpone execution. Examples of use are as follows:

//Background execution:Dispatch_async(Dispatch_get_global_queue (0,0), ^{//Something});//Main thread execution:Dispatch_async(Dispatch_get_main_queue (), ^{//Something});//Disposable Execution:Static dispatch_once_tOncetoken;dispatch_once(&oncetoken, ^{//code to be executed once});//Delay 2 seconds Execution:DoubleDelayinseconds =2.0;d ispatch_time_t poptime = Dispatch_time (Dispatch_time_now, Delayinseconds * nsec_per_sec);d ispatch_after (PopTime, Dispatch_get_main_queue (), ^ (void){//code to being executed on the main queue after delay});

Dispatch_queue_t can also be defined by itself, if you want to customize the queue, you can use the Dispatch_queue_create method, an example is as follows:

dispatch_queue_t urls_queue = dispatch_queue_create("blog.devtang.com"NULL);dispatch_async(urls_queue, ^{     // your code});dispatch_release(urls_queue);

In addition, GCD has some advanced usage, such as having 2 threads in the background executing in parallel, and then summarizing the execution results after 2 threads have finished. This can be achieved with Dispatch_group, Dispatch_group_async, and Dispatch_group_notify, as shown in the following example:

group = dispatch_group_create();dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{     // 并行执行的线程一});dispatch_group_async(group, dispatch_get_global_queue(0,0), ^{     // 并行执行的线程二});dispatch_group_notify(group, dispatch_get_global_queue(0,0), ^{     // 汇总结果});

iOS Development-multithreaded programming (GCD)

Related Article

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.