Brief introduction
GCD task function (whether to open a new thread)
-
Synchronization
- does not have the ability to open a new thread
-
Function to perform tasks synchronously
-
void Dispatch_sync (dispatch_queue_t Queue, dispatch_block_t block), block type
- queue: Task Queue
- block (code block): task performed
-
Void Dispatch_sync_f (dispatch_queue_t queue, void *context, dispatch_function_t work), function type (each block type corresponds to a Type of function)
- queue: Task Queue
- Context: Parameters passed to task function
- work (function): task performed
-
Other functions that perform the task synchronously (barrier) are executed after the previous task has been executed, and the task behind it executes after it executes
-
void Dispatch_barr Ier_sync (dispatch_queue_t queue, dispatch_block_t block), block type
- queue: Task queue, which is meaningful only if the parameter is a concurrent queue
-
Dispatch_barrier_sync_f (dispatch_queue_t queue, void *context, dispatch_function_t work), function class Type
Asynchronous
- Ability to open new threads (need to add tasks to concurrent queues)
Functions that perform tasks asynchronously (parameter meanings are the same as synchronous functions)
- void Dispatch_async (dispatch_queue_t queue, dispatch_block_t block)
- void Dispatch_async_f (dispatch_queue_t queue, void *context, dispatch_function_t work)
Other functions that perform the task asynchronously (barrier) are executed before the previous task executes, and the task behind it executes after it executes (the parameter meaning is the same as the synchronization function)
- void Dispatch_barrier_async (dispatch_queue_t queue, dispatch_block_t block)
- void Dispatch_barrier_async_f (dispatch_queue_t queue, void *context, dispatch_function_t work)
GCD Queue (how tasks are executed)
Concurrent queues
- Turn on multiple threads to enable multiple tasks in the queue to execute concurrently (mates that require the function to be executed asynchronously)
Serial queue
- The tasks in the queue are executed one by one sequentially
Types of queues
Combination of tasks and queues
Other tasks of GCD
-
Single execution (usually in the design of singleton mode)
//define a tag static dispatch_once_t oncetoken; Dispatch_once (&oncetoken, ^{//the code here will only be executed once});
-
Deferred execution
/**-method One (GCD) */dispatch_after ( Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * NSEC_PER_ SEC)), Dispatch_get_main_queue (), ^{//the code here will delay execution}); /**-Method II (Performselector) */[self Performselector: @selector (run) withobject:self Afterdelay: 2.0]; /**-method Three (Nstimer) */[nstimer Scheduledtimerwithtimeinterval:2.0 target:self selector:< Span class= "Hljs-keyword" > @selector (run) userinfo:nil repeats: no]
-
Quick Iteration
void Dispatch_apply (size_t iterations, dispatch_queue_t queue, void (^block) (size_t)) /**iterations: Number of iterations executed queue: Task Queue block: Code for iteration Execution Size_ T: Used to define the current iteration to the number of times, you need to add it yourself, such as adding the index index after size_t, recording the current iteration count */
Barrier
/**- Barrier中的任务,只能在它前面的任务执行完毕才能执行 Barrier后的任务,只能等到它执行完毕才能执行- 要将队列添加到自己创建的并发队列中,否其功能等同于函数 void dispatch_async(dispatch_queue_t queue, dispatch_block_t block)*///创建队列(通常是自己创建的并发队列)dispatch_queue_t queue = dispatch_queue_create("123", DISPATCH_QUEUE_CONCURRENT);//将任务添加到队列dispatch_async(queue, ^{ //在Barrier前执行的任务代码});dispatch_barrier_async(queue, ^{ //Barrier中的任务代码});dispatch_async(queue, ^{ //在Barrier后执行的任务代码});
Queue Group
//get global concurrency queue dispatch_queue_t Queue = dispatch_get_global_queue (Dispatch_queue_priority_default, 0); //Create queue Group dispatch_group_t group = Dispatch_group_create (); //Add task to queue Group Dispatch_group_async (group, queue, ^{//Task 1 code});d Ispatch_group_async (group, queue, ^{//Task 2 code});d Ispatch_group_ Notify (group, queue, ^{//task 3 code /** All tasks in the group are executed if group is empty, execute immediately */});
GCD Timer
Implementation principle
- Creates a Dispatch_source_type_timer type of dispatch source and adds it to the dispatch queue, responding to events by dispatch SOURCE
- by function void Dispatch_source_set_timer (dispatch_source_t source, dispatch_time_t start, uint64_t interval, uint64_t leeway ) To set the execution event for the dispatch source
Implementation code
Get queuedispatch_queue_t queue = Dispatch_get_main_queue ();Create a TimerSelf. Timer = Dispatch_source_create (Dispatch_source_type_timer,0,0, queue);Set the various properties of the timer (starting and ending time) dispatch_time_t start = Dispatch_time (Dispatch_time_now, (int64_t) (8.0 * nsec_per_sec)); uint64_t interval = (uint64_t) (1.0 * nsec_per_sec); Set Dispatch_source_set_timer (self. Timer, start, interval, 0); Set the callback Dispatch_source_set_event_handler (self. Timer, ^{ //Timer to execute code when it is triggered}); Turn on timer dispatch_resume (self. timer); Cancel Timer dispatch_cancel (self. timer);
Oc-19.gcd