What is GCD
GCD is an apple of the multi-threaded programming of a new set of abstractions based on the C language layer of API, combined with block simplifies the multi-threaded operation, so that we can be more safe and efficient threading operations.
Before the advent of GCD, the cocoa framework provided the NSObject class
Performselectorinbackground:withobject
Performselectoronmainthread
method to simplify multithreaded programming techniques.
GCD can address the frequently occurring problems in the following multithreaded programming:
1. Data competition (e.g. updating a memory address at the same time)
2. Deadlock (wait for each other)
3. Too many threads cause a lot of memory consumption
In iOS, if you put a lot of time on the main thread, it will prevent the main thread is called the runloop of the execution of the primary loop, resulting in the inability to update the user interface, the application's screen for a long time stagnation and so on.
Dispatch Queue
The Dispatch queue is a GCD that performs processing of the abstract queues (FIFO) for tasks.
The queue is divided into two types,
Serial_dispatch_queue wait for execution to finish in process now
Concurrent_dispatch_queue does not wait for execution to finish in process now
In other words, Serial_dispatch_queue is serial, concurrent_dispatch_queue is parallel.
specifically to the thread, Serial_dispatch_queue will only create a thread to process the task sequence, while Concurrent_dispatch_queue will be created on multiple threads, but the number of specific creation is a running operating system based on the resources.
So the code processed in Serial_dispatch_queue is ordered, and the concurrent_dispatch_queue is unordered, but relatively more efficient.
Api
Dispatch_queue_create
Used to create a task to execute a queue
Parameter list
The name of the const char *label queue, as the only indicator of the queue, will be displayed directly as Dispatchqueue name in the debugger of Xcode and instruments
dispatch_queue_attr_t sets the type of queue, that is, concurrentqueue or serialqueue,null, the default is Serialqueue
return value
dispatch_queue_t variable
Here's a look at the two systems of Main_dispatch_queue and Global_dispatch_queue,
Main_queue through
Dispatch_get_main_queue ()
Global_queue through
Dispatch_get_global_queue (), the global grade is divided into
Four kinds of high, DEFAULT, low, background
Dispatch_after
Similar to the delay function, you can specify a queue for deferred operation
dispatch_time_t time = Dispatch_time (Dispatch_time_now, 3ull * nsec_per_sec); ^{ NSLog (@ " wait 3 seconds "); });
Dispatch_group_notify
For monitoring queue execution, callbacks can be performed when all tasks are completed
dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); dispatch_group_t Group=dispatch_group_create (); Dispatch_group_async (group, queue,^{NSLog (@"1"); }); Dispatch_group_async (group, queue,^{NSLog (@"2"); }); Dispatch_group_async (group, queue,^{NSLog (@"3"); }); Dispatch_group_notify (group, queue,^{NSLog (@"Finish"); });
For a series of blocks to be executed in the same queue, if the serialqueue is sequential, the end operation can be processed at the last task. But for the concurrentqueue is parallel, if you want to listen to the end operation, it is necessary to use this method.
Dispatch_group_wait and notify are similar, except that the wait method can set the waiting time. If the time has not ended all of the queue's operations, then will continue to proceed, but still can be set to forever has been waiting, so that the Notify play the same role.
Dispatch_barrier_async
This is done primarily to prevent resource contention. In Concurrentqueue, all blocks are unordered at the same time as the number of threads created. If there are two writes in the Concurrentqueue, and he is a read operation, there is a resource contention between the two writes, and the read operation reads the dirty data. Therefore, blocks that cannot be paralleled with other operations in Concurrentqueue need to use the Dispatch_barrier_async method to prevent resource contention.
iOS multithreaded--GCD article