Preliminary understanding of GCD and understanding of GCD

Source: Internet
Author: User

Preliminary understanding of GCD and understanding of GCD

// (1) use an asynchronous function to add tasks to the concurrent queue. // conclusion: Enable three subthreads at the same time-(void) test1 {// 1. obtain the global concurrent queue dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 2. after adding a task to the queue, You can execute the task // asynchronous function: With the ability to enable the new thread dispatch_async (queue, ^ {NSLog (@ "Download image 1 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Download Image 2 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Download Image 3 ---- % @", [NSThread currentThread]) ;}); // print the main thread NSLog (@ "main thread ---- % @", [NSThread mainThread]);}
// (2) use an asynchronous function to add a task to the serial queue // conclusion: the thread is enabled, but only one thread is enabled-(void) test2 {// print the main thread NSLog (@ "main thread ---- % @", [NSThread mainThread]); // create a serial queue dispatch_queue_t queue = dispatch_queue_create ("tqh ", NULL); // The first parameter is the name of the serial queue, which is a C string // The second parameter is the queue attribute. Generally, no attribute is assigned to the serial queue, therefore, a NULL value (NULL) is usually input. // 2. add the task to the queue and execute dispatch_async (queue, ^ {NSLog (@ "Download image 1 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {NSLog (@ "Download Image 2 ---- % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {NSLog (@ "Download Image 3 ---- % @", [NSThread currentThread]) ;}); // 3. release resources // dispatch_release (queue );}
// 3) use the synchronous function to add tasks to the concurrent queue // conclusion: no new threads are enabled, and the concurrent queue loses the concurrent function-(void) test3 {// print the main thread NSLog (@ "main thread ---- % @", [NSThread mainThread]); // create a serial queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0 ); // 2. add a task to the queue and execute dispatch_sync (queue, ^ {NSLog (@ "Download image 1 ---- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Download Image 2 ---- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Download Image 3 ---- % @", [NSThread currentThread]) ;});}
// (4) use the synchronous function to add tasks to the serial queue // conclusion: no new thread is enabled-(void) test4 {NSLog (@ "add a task to the serial queue using the synchronization function"); // print the main thread NSLog (@ "main thread ---- % @", [NSThread mainThread]); // create a serial queue dispatch_queue_t queue = dispatch_queue_create ("tqh", NULL); // 2. add a task to the queue and execute dispatch_sync (queue, ^ {NSLog (@ "Download image 1 ---- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Download Image 2 ---- % @", [NSThread currentThread]) ;}); dispatch_sync (queue, ^ {NSLog (@ "Download Image 3 ---- % @", [NSThread currentThread]) ;});}
/** Supplement: queue name: in future debugging, you can see the queue in which the task is executed. Note: Synchronous functions do not have the ability to enable threads. threads are not enabled in any queue. asynchronous functions can enable threads, the number of threads enabled is determined by the queue (only one new thread is enabled for the serial queue and multiple threads are enabled for the concurrent Queue ). Synchronous function (1) Concurrent queue: no thread (2) Serial queue: no thread asynchronous function (1) Concurrent queue: N threads enabled (2) Serial queue: enable one thread supplement: All functions with the words "create \ copy \ new \ retain" in their names need to be release when this data is not needed. The data type of GCD does not need to be release in the ARC environment. The data type of CF (core Foundation) still needs to be release in the ARC environment. Asynchronous functions have the ability to open threads, but do not necessarily open threads */

 

 

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.