iOS multithreaded Development gcd common usage

Source: Internet
Author: User

1- deferred execution

can schedule its thread (1), the home row

dispatch_queue_t queue= dispatch_get_main_queue ();

Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec)), queue, ^{

NSLog (@ "Home row - deferred execution ------%@", [Nsthread CurrentThread]);

       });      

can schedule its threads (2), concurrent queues

1. Get global concurrent queues

dispatch_queue_t queue1= dispatch_get_global_queue (Dispatch_queue_priority_default, 0);

2. Calculate time for task execution

dispatch_time_t When=dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec));

3. The task in the queue will be executed at this point in time

Dispatch_after (when, queue1, ^{

NSLog (@ " concurrent Queue - deferred execution ------%@", [Nsthread CurrentThread]);

});

2. use dispatch_once Disposable code

Use the Dispatch_once function to ensure that a piece of code is executed only 1 times during program Operation

Static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

Execute code only 1 times ( This is thread-safe by default )

});

The entire program runs, only once.

3. Queue Groups

The use of queue groups allows task 1 and Task 2 to be performed simultaneously, and when both tasks are completed, return to the main thread for interface display operations.

1. Create a queue group

dispatch_group_t group = dispatch_group_create();

2. Start a task

dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ /c15>

});

3. Open a second task

dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{   

});

perform task 1\ Task 2 Operations simultaneously

4. All tasks in the group are completed, and then go back to the main thread to perform other operations

dispatch_group_notify(group,Dispatch_get_main_queue(), ^{

});

iOS multithreaded Development gcd common usage

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.