One, Dispatch Queue
Dispatch_async (queue, ^{ / * * * tasks to be performed */ });
The queue is divided into two types:
1,serial Dispatch Queue waits for execution to finish in the process now.
2,concurrent Dispatch Queue does not wait for execution to finish in the process now.
Two, dispatch_queue_create
Used to generate a queue.
1, generate serial Dispatch Queue.
dispatch_queue_t myserialdispatchqueue=dispatch_queue_create ("Com.example.gcd.MySerialDispatchQueue", NULL);
2, generate concurrent Dispatch Queue.
dispatch_queue_t myconcurrentdispatchqueue=dispatch_queue_create ("Com.example.gcd.MyConcurrentDispatchQueue", Dispatch_queue_concurrent);
3, usage
dispatch_queue_t myconcurrentdispatchqueue=dispatch_queue_create ("Com.example.gcd.MyConcurrentDispatchQueue", Dispatch_queue_concurrent);
Dispatch_async (Myconcurrentdispatchqueue, ^{ NSLog (@ "block on Myconcurrentdispatchqueue");
Three, Main Dispatch Queue/global Dispatch Queue
1,main Dispatch Queue.
The dispatch Queue that is executed in the main thread.
Main Dispatch Queue Acquisition method dispatch_queue_t Maindisaptchqueue=dispatch_get_main_queue () ;
2,global Dispatch Queue.
The dispatch Queue that can be used in all applications. It has 4 priority levels.
Global Dispatch Queue Acquisition method //High-priority dispatch_queue_t Globaldispatchqueuehigh=dispatch_get_global_queue ( Dispatch_queue_priority_high, 0); Default Priority dispatch_queue_t Globaldispatchqueuedefault=dispatch_get_global_queue (dispatch_queue_priority_ DEFAULT, 0); Low-priority dispatch_queue_t Globaldispatchqueuelow=dispatch_get_global_queue (dispatch_queue_priority_low, 0); Background priority dispatch_queue_t Globaldispatchqueuebackground=dispatch_get_global_queue (dispatch_queue_priority_ BACKGROUND, 0);
3, usage
In the default priority global Dispatch queue, execute block dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_ DEFAULT, 0), ^{ / * * processing can be performed in parallel * /Dispatch_async (Dispatch_get_main_queue (), ^{/ * * Processing that can only be performed in the main thread */ });
Four, Dispatch_set_target_queue
Used to change the build of the dispatch queue.
The generation method of the serial Dispatch queue that performs action processing in the background dispatch_queue_t myserialdispatchqueue=dispatch_queue_create (" Com.example.gcd.MySerialDispatchQueue ", NULL); dispatch_queue_t globaldispatchqueuebackground=dispatch_get_global_queue (dispatch_queue_priority_background, 0); Dispatch_set_target_queue (Myserialdispatchqueue, Globaldispatchqueuebackground);
Five, Dispatch_after
Executes the processing after the specified time.
1,ull is the numeric literal of the C language, which is the string used to display the representation type (meaning "unsigned long"). If you use Nsec_per_msec, you can calculate in milliseconds. The number of nanoseconds if Nsec_per_sec is used.
2,dispatch_time_t calculates the relative time. The dispatch_walltime is used to calculate absolute time.
Appends the specified block to the main Dispatch Queue after 3 seconds.
dispatch_time_t time=dispatch_time (Dispatch_time_now, 3ull*nsec_per_sec); Dispatch_after (Time, Dispatch_get_main_queue (), ^{ NSLog (@ "waited at least three seconds.");
VI, Dispatch Group
No matter what kind of dispatch queue you are appending to, you can use dispatch group to monitor the end of these processing executions. Once all execution finishes are detected, the end processing can be appended to the dispatch queue. This is why you use the Dispatch group.
1, add 3 blocks to the global Dispatch queue, which, if fully executed, executes the block that ends processing in the main Dispatch Queue.
Add 3 blocks to the global Dispatch queue, which, if fully executed, executes the block that ends processing in the main Dispatch Queue. 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 (@ "blk0"); }); Dispatch_group_async (group, queue, ^{ NSLog (@ "Blk1"); }); Dispatch_group_async (group, queue, ^{ NSLog (@ "Blk2"); }); Dispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ NSLog (@ "Done"); });
2, use the Dispatch_group_wait function in the dispatch group to wait for the end of all processing execution.
Use the Dispatch_group_wait function in the dispatch group to wait for the end of all processing execution. 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 (@ "blk0"); }); Dispatch_group_async (group, queue, ^{ NSLog (@ "Blk1"); }); Dispatch_group_async (group, queue, ^{ NSLog (@ "Blk2"); }); Dispatch_group_wait (group, dispatch_time_forever);
Seven, Dispatch_barrier_async
In conjunction with the concurrent Dispatch queue, Dispatch) Barrier_async functions enable efficient database access and file access.
1, usage.
After the blk_for_reading read operation, the write processing is added.
Blk3_for_reading after adding write processing. //dispatch_barrier_async waits until the processing of the parallel execution on the concurrent dispatch queue is complete, appending the specified processing to the concurrent dispatch queue. The Concurrent Dispatch queue is then reverted to a normal action after the processing that is appended by the Dispatch_barrier_async function is completed. dispatch_queue_t queue=dispatch_queue_create ("Com.example.gcd.ForBarrier", dispatch_queue_concurrent); Dispatch_async (queue, blk0_for_reading); Dispatch_async (queue, blk1_for_reading); Dispatch_async (queue, blk2_for_reading); Dispatch_async (queue, blk3_for_reading); Dispatch_barrier_async (queue, blk_for_writing); Dispatch_async (queue, blk4_for_reading); Dispatch_async (queue, blk5_for_reading); Dispatch_async (queue, blk6_for_reading); Dispatch_async (queue, blk7_for_reading);
BA, Dispatch_sync
"Non-synchronous", easy to form a deadlock.
Three types of deadlock conditions:
Dispatch_sync //deadlock 1 dispatch_queue_t queue=dispatch_get_main_queue (); Dispatch_sync (queue, ^{ NSLog (@ "Hello"); }); Deadlock 2 dispatch_queue_t queue=dispatch_queue_create ("Com.example.gcd.MySerialDispatchQueue", NULL); Dispatch_async (queue, ^{ dispatch_sync (queue, ^{ NSLog (@ "Hello");} ); Deadlock 3 dispatch_queue_t queue=dispatch_get_main_queue (); Dispatch_async (queue, ^{ dispatch_sync (queue, ^{ NSLog (@ "Hello");} );
Nine, dispatch_apply.
The Dispatch_apply function is the associated API for the Dispatch_sync function and dispatch group. The function appends the specified block to the specified dispatch queue for a specified number of times and waits for the completion of all processing execution.
Usage:
dispatch_queue_t queue=dispatch_get_global_queue (dispatch_queue_priority_default, 0); Dispatch_apply (n, queue, ^ (size_t index) { NSLog (@ "%ld", index); }); NSLog (@ "Done");
Ten, Dispatch_suspend/dispatch_resume.
After suspending, processing that is appended to the dispatch queue but not yet executed is stopped after this. Recovery allows these processes to continue to execute.
Usage:
Suspends the specified dispatch queue dispatch_suspend (queue); Restores the specified dispatch queue. dispatch_resume (queue);
11, Dispatch Semaphore.
The Dispatch semaphore is a signal holding the count, which is a count type signal in multithreaded programming. The so-called signals, similar to those commonly used when crossing the road
Hand flag. In dispatch semaphore, this function is implemented using a count. The count is 0 o'clock wait, the count is 1 or greater than 1 o'clock, minus 1 without waiting.
1, Usage:
dispatch_time_t time=dispatch_time (Dispatch_time_now, 1ull*nsec_per_sec); Long result=dispatch_semaphore_wait (semaphore, time); if (result==0) { //count = 0 o'clock wait. //Can be processed with exclusive control required } else{ //count is 1 or greater than 1 o'clock, minus 1 without waiting. }
2, its benefits.
Original code:
This code uses the global Dispatch queue to update the Nsmutablearray class object, so there is a high probability that a memory error after execution causes the application to end unexpectedly. You should use dispatch Semaphore at this time. dispatch_queue_t queue=dispatch_get_global_queue (dispatch_queue_priority_default, 0); Nsmutablearray *array=[[nsmutablearray Alloc]init]; for (int i=0; i<10000; ++i) { dispatch_async (queue, ^{ [array addobject:[nsnumber numberwithint:i]; }); }
Modified code:
Modified Code dispatch_queue_t queue=dispatch_get_global_queue (dispatch_queue_priority_default, 0); The count initial value of Dispatch semaphore is set to "1". Guaranteed access to the thread of the Nsmutablearray class object, with only 1 dispatch_semaphore_t semaphore=dispatch_semaphore_create (1); Nsmutablearray *array=[[nsmutablearray Alloc]init]; for (int i=0; i<10000; ++i) { dispatch_async (queue, ^{ //waits until the count value of dispatch semaphore reaches greater than or equal to 1. Dispatch_semaphore_wait (semaphore, dispatch_time_forever); After the count of Dispatch semaphore reaches 1, it starts subtracting 1 without waiting. At this point, Semaphore minus 1 becomes 0. [Array Addobject:[nsnumber numberwithint:i]; Add a count of semaphore to 1. Dispatch_semaphore_signal (semaphore); });
12, Dispatch_once.
Ensure that only the specified processing API is executed once in the application execution.
Original code:
Original code static int initialized=no; if (initialized==no) { //Initialize initialized=yes; }
Modified code:
Modified code static dispatch_once_t pred; Dispatch_once (&pred, ^{ //Initialize });
13, Dispatch I/O.
When reading large files, if you divide the files into appropriate sizes and use the global Dispatch queue to read them in parallel, it should be more readable than normal.
A lot faster. Now the input/output hardware has been able to use more than one thread at a time to read the parallel more quickly. The ability to do this is dispatch I/O and dispatch Data.
Usage:
When reading and writing files through Dispatch I/O, use the global Dispatch queue to Read/write 1 files at a certain size. Dispatch_async (queue, ^{/* read 0~8191 bytes */}); Dispatch_async (queue, ^{/* read 8192~16383 bytes */}); Dispatch_async (queue, ^{/* read 16384~24575 bytes */}); Dispatch_async (queue, ^{/* read 24576~32767 bytes */}); Dispatch_async (queue, ^{/* read 32768~40959 bytes */}); Dispatch_async (queue, ^{/* read 40960~49151 bytes */}); Dispatch_async (queue, ^{/* read 49152~57343 bytes */}); Dispatch_async (queue, ^{/* read 57344~65535 bytes */});
Reference: "Objective-c advanced Programming iOS and OS X multithreading and memory Management"
"Reading notes" Gcd-api