概述
Execute code concurrently on multicore hardware by submitting work to dispatch queues managed by the system.
Grand Central Dispatch (GCD) comprises language features, runtime libraries, and system enhancements that provide systemic, comprehensive improvements to the support for concurrent code execution on multicore hardware in macOS, iOS, watchOS, and tvOS.
The BSD subsystem, Core Foundation, and Cocoa APIs have all been extended to use these enhancements to help both the system and your application to run faster, more efficiently, and with improved responsiveness. Consider how difficult it is for a single application to use multiple cores effectively, let alone doing it on different computers with different numbers of computing cores or in an environment with multiple applications competing for those cores. GCD, operating at the system level, can better accommodate the needs of all running applications, matching them to the available system resources in a balanced fashion.
串行和并行隊(duì)列(dispatch queue)
GCD provides and manages FIFO queues to which your application can submit tasks in the form of block objects. Work submitted to dispatch queues are executed on a pool of threads fully managed by the system. No guarantee is made as to the thread on which a task executes.
Serial and Concurrent Queues
A dispatch queue can be either serial, so that work items are executed one at a time, or it can be concurrent, so that work items are dequeued in order, but run all at once and can finish in any order. Both serial and concurrent queues process work items in first in, first-out (FIFO) order.
System-Provided Queues
When an app launches, the system automatically creates a special queue called the main queue. Work items enqueued to the main queue execute serially on your app’s main thread. You can access the main queue using the main type property.
Attempting to synchronously execute a work item on the main queue results in dead-lock.
In addition to the serial main queue, the system also creates a number of global concurrent dispatch queues. You can access the global concurrent queue that best matches a specified quality of service (QoS) using the global(attributes:) type method.
串行隊(duì)列(serial)
串行隊(duì)列會(huì)等待上一個(gè)任務(wù)執(zhí)行完成诅挑,才去開始執(zhí)行下一個(gè)任務(wù)
并行隊(duì)列(concurrent)
并行隊(duì)列會(huì)不需要等待上一個(gè)任務(wù)執(zhí)行完成,而是所有的任務(wù)可以同時(shí)開始執(zhí)行
同步和異步任務(wù)(Synchronous and Asynchronous Execution)
Each work item can be executed either synchronously or asynchronously. When a work item is executed synchronously with the sync method, the program waits until execution finishes before the method call returns. When a work item is executed asynchronously with the async method, the method call returns immediately.
同步任務(wù)(sync)
執(zhí)行sync函數(shù)以block的形式添加一個(gè)任務(wù)到隊(duì)列當(dāng)中摆舟,并阻塞當(dāng)前線
程稽穆,等到block任務(wù)中的執(zhí)行完成sync函數(shù)才會(huì)返回(return)
異步任務(wù)(async)
執(zhí)行async函數(shù)以block的形式添加一個(gè)任務(wù)到隊(duì)列當(dāng)中管毙,async函數(shù)直接返回(return)
死鎖(dead-lock)
串行隊(duì)列的同步任務(wù)中怀吻,添加同步任務(wù)到當(dāng)前隊(duì)列(同一個(gè)串行隊(duì)列)中就會(huì)產(chǎn)生死鎖
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)1開始");
dispatch_sync(serialQueue, ^{
NSLog(@"任務(wù)2開始");
});
NSLog(@"任務(wù)1結(jié)束");
});
分析:
1蝉仇、執(zhí)行最外層dispatch_sync函數(shù)添加任務(wù)1到串行隊(duì)列(serialQueue)中轧钓,等待這個(gè)dispatch_sync函數(shù)返回(return)
2洼裤、開始執(zhí)行隊(duì)列中的任務(wù)1
3邻辉、打印“任務(wù)1開始”
4、執(zhí)行任務(wù)1中的dispatch_sync函數(shù)添加任務(wù)2到串行隊(duì)列(serialQueue)中,等待這個(gè)dispatch_sync函數(shù)返回(return)
5值骇、此時(shí)莹菱,添加任務(wù)1的dispatch_sync函數(shù)要返回(return),就必須等待添加任務(wù)2的dispatch_sync函數(shù)要返回(return)吱瘩,此時(shí)“任務(wù)1等待任務(wù)2完成”道伟;而任務(wù)1和任務(wù)2都被添加到了同一個(gè)串行隊(duì)列(serialQueue)中,而串行隊(duì)列要執(zhí)行下一個(gè)任務(wù)使碾,必須等待上一個(gè)任務(wù)完成蜜徽,而任務(wù)1先與任務(wù)2添加,所以要等待任務(wù)2執(zhí)行票摇,就需要任務(wù)1先執(zhí)行完成拘鞋,此時(shí)就是“任務(wù)2等待任務(wù)1完成”。如此任務(wù)1和任務(wù)2相互等待矢门,就會(huì)造成死鎖