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)系作者
  • 序言:七十年代末盆色,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子祟剔,更是在濱河造成了極大的恐慌隔躲,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,042評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件峡扩,死亡現(xiàn)場(chǎng)離奇詭異蹭越,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)教届,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 89,996評(píng)論 2 384
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來驾霜,“玉大人案训,你說我怎么就攤上這事》嗖冢” “怎么了强霎?”我有些...
    開封第一講書人閱讀 156,674評(píng)論 0 345
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蓉冈。 經(jīng)常有香客問我城舞,道長(zhǎng),這世上最難降的妖魔是什么寞酿? 我笑而不...
    開封第一講書人閱讀 56,340評(píng)論 1 283
  • 正文 為了忘掉前任家夺,我火速辦了婚禮,結(jié)果婚禮上伐弹,老公的妹妹穿的比我還像新娘拉馋。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,404評(píng)論 5 384
  • 文/花漫 我一把揭開白布煌茴。 她就那樣靜靜地躺著随闺,像睡著了一般。 火紅的嫁衣襯著肌膚如雪蔓腐。 梳的紋絲不亂的頭發(fā)上矩乐,一...
    開封第一講書人閱讀 49,749評(píng)論 1 289
  • 那天,我揣著相機(jī)與錄音回论,去河邊找鬼绰精。 笑死,一個(gè)胖子當(dāng)著我的面吹牛透葛,可吹牛的內(nèi)容都是我干的笨使。 我是一名探鬼主播,決...
    沈念sama閱讀 38,902評(píng)論 3 405
  • 文/蒼蘭香墨 我猛地睜開眼僚害,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼硫椰!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起萨蚕,我...
    開封第一講書人閱讀 37,662評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤靶草,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后岳遥,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體奕翔,經(jīng)...
    沈念sama閱讀 44,110評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,451評(píng)論 2 325
  • 正文 我和宋清朗相戀三年浩蓉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了派继。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,577評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡捻艳,死狀恐怖驾窟,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情认轨,我是刑警寧澤绅络,帶...
    沈念sama閱讀 34,258評(píng)論 4 328
  • 正文 年R本政府宣布,位于F島的核電站嘁字,受9級(jí)特大地震影響恩急,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纪蜒,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,848評(píng)論 3 312
  • 文/蒙蒙 一衷恭、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧霍掺,春花似錦匾荆、人聲如沸拌蜘。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,726評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽简卧。三九已至,卻和暖如春烤芦,著一層夾襖步出監(jiān)牢的瞬間举娩,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,952評(píng)論 1 264
  • 我被黑心中介騙來泰國(guó)打工构罗, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留铜涉,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,271評(píng)論 2 360
  • 正文 我出身青樓遂唧,卻偏偏與公主長(zhǎng)得像芙代,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子盖彭,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,452評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容