Take You system learning GCD (ii)

Source: Internet
Author: User

Welcome into the coding world of summer. 1. Dispatch_barrier_async

He is explained by the code below.

    dispatch_queue_tQueue = Dispatch_queue_create ("Queue.example", dispatch_queue_concurrent);Dispatch_async(Queue, ^{NSLog(@"-------------------1"); });Dispatch_async(Queue, ^{NSLog(@"-------------------2"); });Dispatch_async(Queue, ^{NSLog(@"-------------------3"); });Dispatch_async(Queue, ^{NSLog(@"-------------------4"); });//Dispatch_barrier_async (queue, ^{        //NSLog (@ "I am a Barrier");        //    });    Dispatch_async(Queue, ^{NSLog(@"-------------------5"); });Dispatch_async(Queue, ^{NSLog(@"-------------------6"); });Dispatch_async(Queue, ^{NSLog(@"-------------------7"); });Dispatch_async(Queue, ^{NSLog(@"-------------------8"); });

When certain features are implemented in the development process, we may need specific representations, such as what I want to do after the completion of the 1234 Execution (5678), so that using Dispatch_barrier_async can be done, Of course, using the group and Dispatch_set_target_queue (less commonly used) in the previous article can also be implemented, but the source code will be very complex, not recommended.

2. Dispatch_sync

The Dispatch_sync function "Async" is asynchronous and non-synchronous, and sync is synchronous.

Suppose a scenario: when executing the main Dispatch queue, use a different thread of the Global Dispatch queue for processing, and immediately use the resulting result code as shown below

dispatch_queue_t0);    dispatch_sync(queue, ^{        //    });

Sync can easily cause a deadlock condition. Below is a list of several
Scenario 1

dispatch_queue_t queue2 = dispatch_get_main_queue();    dispatch_sync(queue2, ^{        //        NSLog(@"----------"// 不会被执行    });

Scenario 2

    dispatch_queue_t queue3 = dispatch_get_main_queue();    dispatch_async(queue3, ^{        //        dispatch_sync(queue3, ^{            NSLog(@"----------");        });    });

Scenario 3

    dispatch_queue_t queue4 = dispatch_queue_create("queue.example", DISPATCH_QUEUE_SERIAL);    dispatch_async(queue4, ^{        //        dispatch_sync(queue4, ^{            //            NSLog(@"---------");        });    });
3. dispatch_apply

Dispatch_apply is the associated API for Dispatch_sync and dispatch Group.
Execute the code in block repeatedly. Examples such as the following

dispatch_queue_t0);    dispatch_apply(5, queue5, ^(size_t i) {        NSLog(@"i= %zu", i);    });

In fact, the dispatch_apply function is the same as the Dispatch_sync function, and will wait for the execution to finish, so it is recommended to use the sample code as follows

    dispatch_queue_tQueue6 = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);//non-synchronous execution in global Dispatch Queue    Dispatch_async(Queue6, ^{//Global Dispatch Queue waits for all processing execution to finish in the Dispatch_apply functionDispatch_apply (5, Queue6, ^ (size_t index) {//            NSLog(@"i =%zu", index); });//The processing in the Dispatch_apply function ends with all execution        //In main Dispatch Queue non-synchronous execution        Dispatch_async(Dispatch_get_main_queue (), ^{//user interface update, etc.            NSLog(@"End");    }); });

Original link

Take You system learning GCD (ii)

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.