Use of some APIs in GCD

Source: Internet
Author: User
Tags gcd

Use of some APIs in GCD 1. Serial Queue and parallel queue

iOS threads are divided into serial queues and parallel queues, and serial queues refer to tasks being executed sequentially and sequentially, while parallel queues refer to the execution of tasks that do not follow the order in which they are joined.
Serial queues in iOS are created in the following ways

dispatch_queue_t queue = dispatch_queue_create("cn.bourbonz.www", DISPATCH_QUEUE_SERIAL);

It is important to note that the content in the quotation marks represents the description of the queue, and the following parameters represent the serial queue.
Parallel queues are created in the following way

dispatch_queue_t queue = dispatch_queue_create("cn.bourbonz.www", DISPATCH_QUEUE_CONCURRENT);或者dispatch_queue_t0);

In the first way, the first parameter represents a description of the queue, and the latter parameter indicates that it is a parallel queue.
In the second way, this queue creates a parallel queue of a global type, the first parameter represents the priority of the current queue, and the second parameter has an unknown meaning.
It is important to note that GCD is a C-language system, so it does not follow the arc mechanism. When creating a queue for

dispatch_queue_create

When this method is used, it needs to be released when the queue is not in use.

dispatch_release
2. Change the priority of the queue

The priority after the queue is created can be changed and the method used to

dispatch_set_target_queue(dispatch_object_t object, dispatch_queue_t queue);

This is the priority of changing the first queue to a second queue

3.dispatch_after
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)dispatch_get_main_queue(), ^{    });

This method is to add the task n seconds after the queue, not the task to join the queue after n seconds to execute, so it is important to note that if the previous task is blocked, then the time here may not be accurate.

4.dispatch_group

This method is done after the tasks of multiple queues have been executed before the following operations are performed, the code is executed as follows

    group= dispatch_group_create();    dispatch_group_async(groupqueue, ^{    });    dispatch_group_async(groupqueue, ^{    });    dispatch_group_notify(group, dispatch_get_main_queue(), ^{    });
5.dispatch_barrier_async

In the process of reading and writing to the file, the read and write operations can only be performed in a single operation.
In GCD, this method can be used to quickly read and write files in a parallel queue, and the operation of this method is that only after the event processing of this method is done, the subsequent operations are performed so that the read and write operations can be separated

    dispatch_async(queue, ^{        //读    });    dispatch_async(queue, ^{        //读    });    dispatch_async(queue, ^{        //读    });    dispatch_barrier_async(queue, ^{       //写    });    dispatch_async(queue, ^{        //读    });    dispatch_async(queue, ^{        //读    });
6.dispatch_sync

Since the function name contains async (asynchronous meaning), then there should be the functions of sync.
The asynchronous queue method means that it can be executed without waiting for the return result of the function;
The synchronous queue method is the result of waiting for the function to be returned, that is, it must wait until the function has finished processing to continue execution.
Then in the process of using a slight careless, there may be a deadlock situation, such as the following conditions

    dispatch_sync(dispatch_get_main_queue(), ^{        NSLog(@"123");    });    dispatch_async(dispatch_get_main_queue(), ^{        dispatch_sync(dispatch_get_main_queue(), ^{            NSLog(@"123");        });    });
7.dispatch_apply

The GCD provides a looping method that can be used for fast traversal,

    dispatch_applyqueue, ^(size_tindex{    });

It is important to note that the execution of this method is not sequential, as in the case of a For loop, and his order is random.

8. Thread hangs and continues
        NSLog(@"线程暂停");        dispatch_suspend(queue);        NSLog(@"线程继续");        dispatch_resume(queue);

It is important to note that you do not perform this operation on the main site to avoid blocking the main thread.

9.dispatch_once

In Apple's documentation, the recommended singleton creation method is created using this method. This method ensures that only one time is executed during a program run.

    staticdispatch_once_t onceToken;    dispatch_once(&onceToken, ^{        ///需要执行的操作    });
Large file read and write in 10.GCD

Dispatch_io_create

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use of some APIs in GCD

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.