The use of dispatch_apply

Source: Internet
Author: User

The use of dispatch_apply

Simulating a For loop in parallel, looping the specified code 10 times, typically attaching the code to a queue and then parallel in the dispatch_apply

dispatch_queue_t queue = Dispatch_get_globel_queue (dispatch_queue_preority_default,0)

dispatch_apply, queue, ^ (size_t index) {

NSLog ("Parallel execution--%@", index);

});

The use of Dispatch_apply_f

Dispatch_apply and Dispatch_apply_f are synchronous functions that block the current thread until all loop iterations are completed. The order in which the loop iterations are executed is indeterminate when committed to a concurrent queue

Dispatch_after

Perform a task after a specified time is delayed

dispatch_after(dispatch_time_t when, dispatch_queue_t queue, ^(void)block)

when:延迟执行时间 一般是一个这样的时间函数: dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC))

Queue: Queues Dispatch_get_main_queue () or Dispatch_get_global_queue ()

Block: Tasks to perform

Dispatch_once

Dispatch_once (dispatch_once_t *predicate, ^ (Void) block)

Predicate: Used to determine whether the task to be performed is completed

Block: A one-time task that needs to be performed

+ (Instantclass *) sharedclient {

  static Instantclass *_sharedclient = nil;

static dispatch_once_t Oncetoken;

Dispatch_once (&oncetoken, ^{

_sharedclient = [[Instantclass alloc] init];

});

   return _sharedclient;

}

Dispatch_group

Dispatch_grpup, put a number of tasks into a dispatch group

dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_preority_default,0);

Dispatch_queue_group group = Dispatch_group_craete ();

DISPATCH_GROUP_AYSNC (Group, queue,^{

Do some async work

});

Dispatch_group_async, like Dispatch_async, would put the task into a queue, except that it was a step more than Dispatch_async, which was to correlate the task with the group,

After putting these tasks into group, we can call dispatch_group_wait to wait for the task to complete. If the task is complete or empty, return directly, otherwise wait for all tasks to be completed. Notice that the task is emptied after returning.

Dispatch_group_wait (Group,dispatch_time_forver);

Dispatch_release (group);

The use of dispatch_apply

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.