GCD

概述

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ì)造成死鎖

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容