Sorting of GCD serial queues, concurrent queues and main queue Columns

Source: Internet
Author: User

 

1. Serial queue: Dispatch queues or SErial queues

Synchronization

 
Dispatch_queue_t queue = dispatch_queue_create ("queue", nil); dispatch_sync (queue, ^ (void) {sleep (2-I/50); nslog (@ "the sum is: % d ", i); // flag = yes;}); dispatch_release (Queue );

Asynchronous

 

Dispatch_queue_t queue = dispatch_queue_create ("queue", nil); dispatch_async (queue, ^ (void) {sleep (2-I/50); nslog (@ "the sum is: % d ", i); // signal the semaphore // dispatch_semaphore_signal (SEM) ;}); dispatch_release (Queue );

When the main thread is waiting for the semaphore, the operating system will directly suspend it, so it will not stop doing sign round-robin, and can promptly allocate CPU resources to other schedulable threads.

 
_ Block queue SEM = Queue (0); dispatch_queue_t queue = dispatch_queue_create ("queue", nil); dispatch_async (queue, ^ (void) {sleep (2-I/50 ); nslog (@ "the sum is: % d", I); // signal the semaphore dispatch_semaphore_signal (SEM) ;}); // wait for the semaphore dispatch_semaphore_wait (SEM, dispatch_time_forever); dispatch_release (Queue );


2.Concurrent queue
: Concurrent queues

 
Dispatch_queue_t aqueue = Queue (queue, 0); dispatch_queue_t ahqueue = dispatch_get_global_queue (queue, 0); dispatch_queue_t alqueue = Queue (queue, 0 );

3. Main queue: Main dispatch queue

 
Dispatch_queue_t mainqueue = dispatch_get_main_queue ();

For the above three Queues: Concurrent queuesAndMain queueBoth are generated by the system andDispatch_suspend, dispatch_resume, dispatch_set_contextThese functions are invalid for them.

However, our applications are not simply synchronous or asynchronous, and applications are often mixed.

For exampleTask1 task2 task3Can be run asynchronously after all the operations are completed.Task4 task5 task6What should we do? Here we can introduceGroup.

-(Void) sixthmethod {// obtain concurrent queue dispatch_queue_t aqueue = Queue (queue, 0); // create a queue group dispatch_group_t queuegroup = dispatch_group_create (); // Task 1 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@ "Task 1. ") ;}); // Task 2 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@" Task 2. ") ;}); // Task 3 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@" Task 3. ") ;}); nslog (@" Wait Task 1, 2, 3. "); // wait for all tasks in the Group to finish dispatch_group_wait (queuegroup, dispatch_time_forever); nslog (@" Task 1, 2, 3 finished. "); // release group dispatch_release (queuegroup); // re-create group queuegroup = dispatch_group_create (); // Task 4 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@ "Task 4. ") ;}); // Task 5 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@" Task 5. ") ;}); // Task 6 dispatch_group_async (queuegroup, aqueue, ^ {nslog (@" Task 6. ") ;}); nslog (@" Wait Task 4, 5, 6. "); // wait for all tasks in the Group to finish dispatch_group_wait (queuegroup, dispatch_time_forever); nslog (@" task, 6 finished. "); // release group dispatch_release (queuegroup );}

CodeRunning result:

13:55:33. 783 GDC [2466: 1303] Task 1.
13:55:33. 783 GDC [2466: 3a07] Task 3.
13:55:33. 783 GDC [2466: c07] Wait task, 3.
13:55:33. 783 GDC [2466: 1903] Task 2.
13:55:33. 787 GDC [2466: c07] task, 3 finished.
13:55:33. 788 GDC [2466: c07] Wait task, 6.
13:55:33. 788 GDC [2466: 1303] Task 4.
13:55:33. 788 GDC [2466: 1903] Task 5.
13:55:33. 788 GDC [2466: 3a07] Task 6.
13:55:33. 790 GDC [2466: c07] task, 6 finished.

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.