Advanced usage of GCD for iOS threads

Source: Internet
Author: User

Advanced usage of GCD for iOS threads

A previous blog on threads has introduced the simple usage and example of GCD. Today, due to the special application of GCD in the project, the following describes how to use GCD with two special requirements.


Purpose: To finish one task, and then perform the next task to ensure the function execution cycle.

Solution: provides the following two solutions:

1. Create a barrier wait thread

Dispatch_async (dispatch_get_main_queue (), ^ {

[Self ActionFirst];

});

Dispatch_barrier_async (dispatch_get_main_queue (), ^ {

[Self ActionNext];

});

2. Create a group thread group (queue)


Dispatch_group_t group = dispatch_group_create ();

Dispatch_group_async (group, dispatch_get_main_queue (), ^ {


[Self ActionFirst];


});

Dispatch_group_policy (group, dispatch_get_main_queue (), ^ {


[Self ActionNext];


});


The error code is as follows:


-(Void) example

{
[Self ActionFirst];

[Self ActionNext];
}

Cause: When the example function is called, because the ActionFirst function has been executed for a long time, when the ActionNext function is executed, the ActionFirst function may not be executed, resulting in a crash when the ActionNext function is executed.

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.