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.