iOS multithreaded operation (v)--GCD serial queue and concurrent queue

Source: Internet
Author: User

the GCD queue can be divided into 2 major types, serial and concurrent queues, respectivelyserial queuing (Serial Dispatchqueue):schedule only one task at a time, and the tasks in the queue execute one after the other (after a task is executed, the next task is executed)Create a queue dispatch_queue_t q = dispatch_queue_create (const char *label, dispatch_queue_attr_t attr) /c10> parameters: const char *label: The name of the queue dispatch_queue_attr_t attr : The properties of the queue, the attributes are two, respectively: dispatch_queue_serial ( NULL) serial queue dispatch_queue_concurrent concurrent queues The queue property is a macro, where the macro value of the serial queue is null, so a serial queue can be created with the following code
dispatch_queue_t q = dispatch_queue_create ("chuanxing", NULL);
A serial asynchronous task demonstrates the following
for (int i = 0; i < ++i) {        //10 async        Dispatch_async (q, ^{            NSLog (@ "%@-%d", [Nsthread currentthread],i) ;        });    }    NSLog (@ "Come here-%@", [Nsthread CurrentThread]);

execution results are as follows

The main thread execution time is not deterministic, but the serial queue that adds 10 asynchronous tasks, in any case, executes one after the other, and only one thread is openedIf you change the task to a synchronous task, the result is predictable (the characteristics of the synchronization task)

Concurrent queues:concurrent queues allow multiple thought witches to execute concurrently (at the same time), which automatically turns on multiple threads to perform tasks concurrently, and concurrency is only valid under asynchronous functions, such as the following code, which performs synchronization tasks under a parallel queue:
1. Queue    dispatch_queue_t q = dispatch_queue_create ("bingxing", dispatch_queue_concurrent);        2. Synchronous execution for    (int i = 0; i < ++i) {        dispatch_sync (q, ^{            NSLog (@ "%@%d", [Nsthread CurrentThread], i); 
   });    }       NSLog (@ "Come here-%@", [Nsthread CurrentThread]);


The results are as follows

executing a synchronization function under a concurrent queue does not create a new thread, and all tasks are executed sequentially on the main thread. and look at the execution of asynchronous functions under the concurrent queue
1. Queue    dispatch_queue_t q = dispatch_queue_create ("Itheima", dispatch_queue_concurrent);        2. Synchronous execution for    (int i = 0; i < ++i) {        dispatch_async (q, ^{            NSLog (@ "%@%d", [Nsthread CurrentThread], i); 
   });    }       NSLog (@ "Come here-%@", [Nsthread CurrentThread]);

The results of the operation are as follows:
It can be concluded that the asynchronous function in the parallel queue will open n sliver threads, and the order of execution of the tasks we cannot control, as to which thread to execute the task is determined by the queue, which task is completed by the CPU. The result is that the child threads of number = 4 and number = 2 perform multiple tasks because the threads are reclaimed by the thread pool after the tasks have been executed, the queue is then removed from the thread pools to perform the task, and the thread is reused, and if no threads are recreated.



iOS multithreaded operation (v)--GCD serial queue and concurrent queue

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.