"Introduction to iOS Multithreading" mentions: there are 2 core concepts in GCD: 1, tasks (what to do) 2, queues (for storing tasks)
So what are the basic uses of multi-threading GCD?
Can be divided into the following situations:
1. Asynchronous function + Concurrent queue
/** * Async function + Concurrent queue: Multiple threads can be opened at the same time*/- (void) asyncconcurrent{//1. Create a concurrent queue//dispatch_queue_create (const char *label, dispatch_queue_attr_t attr); //Label: equivalent to the name of the queue//dispatch_queue_t queue = dispatch_queue_create ("Com.kyle.queue", dispatch_queue_concurrent); //1. Get a global concurrent queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. Join a task to a queueDispatch_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. Synchronization function + Concurrent queue
/** * Synchronization function + Concurrent queue: New threads are not opened*/- (void) syncconcurrent{//1. Get a global concurrent queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. Join a task to a queueDispatch_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 function + Serial queue
/** * Async function + serial queue: A new thread is opened, but the task is serial, executes a task, and then executes the next task*/- (void) asyncserial{//1. Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("Com.kyle.queue", dispatch_queue_serial); //dispatch_queue_t queue = dispatch_queue_create ("Com.kyle.queue", NULL); //2. Join a task to a queueDispatch_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 function + Serial queue
/** * Synchronous function + Serial queue: does not open a new thread and executes a task on the current thread. The task is serial, executes a task, and then executes the next task*/- (void) syncserial{//1. Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("Com.kyle.queue", dispatch_queue_serial); //2. Join a task to a queueDispatch_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. async Function + Home row
//* Async function + Home column: Perform tasks only in the main thread- (void) asyncmain{//1. Get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); //2. Join a task to a queueDispatch_async (Queue, ^{NSLog (@"This is first %@", [Nsthread CurrentThread]); NSLog (@"This is second%@", [Nsthread CurrentThread]); NSLog (@"This is third%@", [Nsthread CurrentThread]); });}
6. Sync function + Home row
//* Sync function + Home column:- (void) syncmain{NSLog (@"begin"); //1. Get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); //2. Join a task to a queueDispatch_sync (Queue, ^{NSLog (@"This is %@", [Nsthread CurrentThread]); }); NSLog (@"End");}
Causing "deadlock of mutual Waiting"
Basic use of IOS multithreaded GCD