Basic use of iOS multi-thread GCD and ios multi-thread gcd
As mentioned in the introduction to iOS multithreading, GCD has two core concepts: 1. Tasks (what operations are performed) 2. Queues (used to store tasks)
What are the basic usage of multithreading GCD?
It can be divided into the following situations:
1. asynchronous functions + concurrent queues
/*** Asynchronous Function + concurrent queue: Multiple Threads can be enabled simultaneously */-(void) asyncConcurrent {// 1. create a concurrent queue // dispatch_queue_create (const char * label, dispatch_queue_attr_t attr); // label: equivalent to the queue name // dispatch_queue_t queue = dispatch_queue_create ("com. kyle. queue ", DISPATCH_QUEUE_CONCURRENT); // 1. obtain the global concurrent queue dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2. add the task to the queue dispatch_async (queue, ^ {for (NSInteger I = 0; I <3; I ++) {NSLog (@ "this is first % @", [NSThread currentThread]) ;}}); dispatch_async (queue, ^ {for (NSInteger I = 0; I <3; I ++) {NSLog (@ "this is second % @", [NSThread currentThread]) ;}}); dispatch_async (queue, ^ {for (NSInteger I = 0; I <3; I ++) {NSLog (@ "this is third % @", [NSThread currentThread]) ;}});}
2. synchronous functions + concurrent queues
/*** Synchronous Function + concurrent queue: no new thread is enabled */-(void) syncConcurrent {// 1. obtain the global concurrent queue dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2. add the task to the queue dispatch_sync (queue, ^ {NSLog (@ "this is first % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "this is second % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "this is third % @", [NSThread currentThread]);}
3. asynchronous functions + Serial queues
/*** Asynchronous Function + Serial queue: New threads are enabled, but tasks are serialized. After a task is executed, the next task is executed */-(void) asyncSerial {// 1. create a serial queue dispatch_queue_t queue = dispatch_queue_create ("com. kyle. queue ", DISPATCH_QUEUE_SERIAL); // dispatch_queue_t queue = dispatch_queue_create (" com. kyle. queue ", NULL); // 2. add the task to the queue dispatch_async (queue, ^ {NSLog (@ "this is first % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {NSLog (@ "this is second % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {NSLog (@ "this is third % @", [NSThread currentThread]);}
4. synchronous functions + Serial queues
/*** Synchronous Function + Serial queue: New threads are not enabled and tasks are executed in the current thread. The task is serialized. After a task is executed, execute the next task */-(void) syncSerial {// 1. create a serial queue dispatch_queue_t queue = dispatch_queue_create ("com. kyle. queue ", DISPATCH_QUEUE_SERIAL); // 2. add the task to the queue dispatch_sync (queue, ^ {NSLog (@ "this is first % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "this is second % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "this is third % @", [NSThread currentThread]);}
5. asynchronous functions + main Columns
// * Asynchronous Function + main queue column: Execute the task only in the main thread-(void) asyncMain {// 1. obtain the main queue column dispatch_queue_t queue = dispatch_get_main_queue (); // 2. add the task to the queue dispatch_async (queue, ^ {NSLog (@ "this is first % @", [NSThread currentThread]); NSLog (@ "this is second % @", [NSThread currentThread]); NSLog (@ "this is third % @", [NSThread currentThread]);}
6. synchronous functions + main Columns
// * Synchronization function + main queue column:-(void) syncMain {NSLog (@ "begin"); // 1. obtain the main queue column dispatch_queue_t queue = dispatch_get_main_queue (); // 2. add the task to the queue dispatch_sync (queue, ^ {NSLog (@ "this is % @", [NSThread currentThread]) ;}); NSLog (@ "end ");}
Causing "deadlocks waiting for each other"