1, the GCD

Grand Central Dispatch (GCD) is a relatively new solution to multi-core programming developed by Apple. It is primarily used to optimize applications to support multicore processors and other symmetric multiprocessing systems. It is a concurrent task executed on a thread pool basis. First released on Mac OS X 10.6 Snow Leopard and also available on iOS 4 and above.Copy the code

The GCD benefits

* GCD can be used for multi-core parallel computing; * GCDS automatically utilize more CPU cores (e.g. Dual-core, quad core); * GCD automatically manages the thread lifecycle (thread creation, task scheduling, thread destruction); * Programmers only need to tell the COMMUNIST party what tasks they want to perform, without writing any thread management code. You need to tell the GCD what tasks you want to perform without writing any thread management code.Copy the code

Queues and tasks

A task is a block of code that needs to be executed, and a queue is a queue. Their relationship is a block of code waiting to be executed.

Synchronous queue execution and asynchronous execution

Synchronous execution means that everyone is executed in the same queue. The next queue can be executed only after the last one is executed. Asynchronous execution means that a peer team can execute multiple tasks at the same time without waiting for the previous task. Asynchronous execution enables multiple threads to process tasks.

usage

Dispatch_queue_t queue = dispatch_get_main_queue(); Dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); Dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_SERIAL); // dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT); Dispatch_sync (queue, ^{// todo}); Dispatch_async (queue, ^{// todo});Copy the code

The use of threads requires attention to deadlocks and related issues

Other GCD methods

Fence method: dispatch_barrier_async

// Isolate the execution of two concurrent queues, Barrier {dispatch_queue_t queue = - (void)barrier {dispatch_barrier_async = dispatch_queue_create("com.cnxq.queue", DISPATCH_QUEUE_CONCURRENT); Dispatch_async (queue, ^{NSThread sleepForTimeInterval:2]) dispatch_async(queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "1 - % @", [NSThread currentThread]); // Prints the current thread}); Dispatch_async (queue, ^{NSThread sleepForTimeInterval:2]) dispatch_async(queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "2 - % @", [NSThread currentThread]); // Prints the current thread}); Dispatch_barrier_async (queue, ^{NSThread sleepForTimeInterval:2]; NSLog(@"barrier-- %@",[NSThread currentThread]); // Prints the current thread}); Dispatch_async (queue, ^{NSThread sleepForTimeInterval:2]) dispatch_async(queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "3 - % @", [NSThread currentThread]); // Prints the current thread}); Dispatch_async (queue, ^{NSThread sleepForTimeInterval:2]) dispatch_async(queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "4 - % @", [NSThread currentThread]); // Prints the current thread}); }Copy the code

“Dispatch_after” is used for delay execution. “dispatch_once” is used for rapid iteration. “dispatch_apply” is used for rapid iteration

// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), dispatch_get_main_queue(), ^{ NSLog(@"after---%@",[NSThread currentThread]); // Prints the current thread}); static dispatch_once_t onceToken; Dispatch_once (&onceToken, ^{// this will only be executed once}); // Blocks are executed asynchronously, Dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); NSLog(@"egin"); dispatch_apply(10, queue, ^(size_t index) { NSLog(@"%zd---%@",index, [NSThread currentThread]); }); NSLog(@"end");Copy the code

Queue group: dispatch_group, dispatch_group_notify

NSLog(@"currentThread---%@",[NSThread currentThread]); // Prints the current thread NSLog(@"group-- --begin"); dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_queue_create("com.cnxq.queue", DISPATCH_QUEUE_CONCURRENT); Dispatch_group_async (group, queue, ^{NSThread sleepForTimeInterval:2]) dispatch_group_async(group, queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "1 - % @", [NSThread currentThread]); // Prints the current thread}); // dispatch_group_wait(group, DISPATCH_TIME_FOREVER); Dispatch_group_async (group, queue, ^{NSThread sleepForTimeInterval:2]) dispatch_group_async(group, queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "2 - % @", [NSThread currentThread]); // Prints the current thread}); ^{dispatch_group_notify(dispatch_get_main_queue(), dispatch_get_main_queue(), ^{ Return to the main thread and perform the following tasks [NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "3 - % @", [NSThread currentThread]); // Prints the current thread NSLog(@"group-- end"); });Copy the code

Semaphore: dispatch_semaphore converts asynchronous tasks to synchronous tasks

dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_semaphore_t semaphore = dispatch_semaphore_create(0); __block int number = 0; Dispatch_async (queue, ^{NSThread sleepForTimeInterval:2]) dispatch_async(queue, ^{NSThread sleepForTimeInterval:2]; / / simulation time-consuming operation NSLog (@ "1 - % @", [NSThread currentThread]); // Print the current thread number = 100; dispatch_semaphore_signal(semaphore); }); dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);Copy the code