Basic use of iOS multi-thread GCD and ios multi-thread gcd

Source: Internet
Author: User

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"

Related Article

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.