Basic use of IOS multithreaded GCD

Source: Internet
Author: User

"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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.