GCD(一) - dispatch_barrier_async

之前總結(jié)過(guò)很多GCD的用法,好久沒(méi)用了,這次再總結(jié)一下.做個(gè)筆記.個(gè)人理解,歡迎交流....

void dispatch_barrier_async( dispatch_queue_t queue, dispatch_block_t block);

文檔解釋:Submits a barrier block for asynchronous execution and returns immediately.
就是這個(gè)函數(shù)提交一個(gè)比較耗時(shí)的代碼塊 到一個(gè)異步操作上,并立即返回.我理解這就是異步操作.

Discussion 詳細(xì)說(shuō)明
Calls to this function always return immediately after the block has been submitted and never wait for the block to be invoked.
我理解就是 提交代碼塊以后,不做任何等待,直接返回.也就是異步.
When the barrier block reaches the front of a private concurrent queue, it is not executed immediately.
當(dāng)這個(gè)代碼塊被提交到 一個(gè)私有的并發(fā)隊(duì)列上的時(shí)候,代碼塊不會(huì)被理解執(zhí)行.
Instead, the queue waits until its currently executing blocks finish executing.
相應(yīng)的是等到這個(gè)隊(duì)列當(dāng)前正在執(zhí)行的代碼塊執(zhí)行完畢以后,才會(huì)執(zhí)行.
At that point, the barrier block executes by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes.
同樣的,在這個(gè)阻塞的代碼塊之后 提交到這個(gè)隊(duì)列上的代碼塊 都不會(huì)被執(zhí)行,直到這個(gè)阻塞的代碼塊執(zhí)行完.

但是這里有個(gè)問(wèn)題:
The queue you specify should be a concurrent queue that you create yourself using the dispatch_queue_create function. If the queue you pass to this function is a serial queue or one of the global concurrent queues, this function behaves like the dispatch_async function.
調(diào)用這個(gè)方法,傳遞的queue 隊(duì)列,需要指定一個(gè)并發(fā)隊(duì)列,并且是 用 dispatch_queue_create 這個(gè)函數(shù)創(chuàng)建的.
否則,如果傳遞的隊(duì)列是一個(gè)串行的隊(duì)列,或者是一個(gè)全局的并發(fā)隊(duì)列.那么這個(gè)函數(shù)的使用效果,就是和一個(gè)簡(jiǎn)單的異步dispatch_async函數(shù)效果一樣了.

看下面代碼傳遞 一個(gè)私有的并發(fā)隊(duì)列:

 let currentQueue:dispatch_queue_t = dispatch_queue_create("com.eric", DISPATCH_QUEUE_CONCURRENT);
    
    var num = 10
    dispatch_barrier_async(currentQueue) {
        sleep(1)
        num = 11
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 12
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 13
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 14
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("5")
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("6")
        print(NSThread.currentThread())
    }
    print("結(jié)束")

打印結(jié)果:
<pre>結(jié)束
11
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}
12
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}
13
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}
14
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}
5
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}
6
<NSThread: 0x12cd1ee80>{number = 2, name = (null)}</pre>

說(shuō)明是異步執(zhí)行,其次在第一個(gè) dispatch_barrier_async(currentQueue) { }中添加sleep()阻塞了線程,后面的 dispatch_barrier_async(currentQueue) {}方法全部被阻塞住了.因?yàn)槲覀儌鬟f的參數(shù)是我們自己創(chuàng)建的一個(gè)私有隊(duì)列.

但是如果我們傳遞的queue是其它的,上面提到的全局并發(fā)隊(duì)列或者串行化隊(duì)列看下代碼:
let currentQueue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    var num = 10
    dispatch_barrier_async(currentQueue) {
        sleep(1)
        num = 11
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 12
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 13
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 14
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("5")
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("6")
        print(NSThread.currentThread())
    }
    print("結(jié)束")

執(zhí)行結(jié)果:
<pre>結(jié)束
14
14
14
5
6
<NSThread: 0x12de57f80>{number = 3, name = (null)}
<NSThread: 0x12de16800>{number = 2, name = (null)}
<NSThread: 0x12de5b4b0>{number = 4, name = (null)}
<NSThread: 0x12de5dde0>{number = 5, name = (null)}
<NSThread: 0x12de512c0>{number = 6, name = (null)}
11
<NSThread: 0x12de2bfd0>{number = 7, name = (null)}
</pre>
很明顯可以看到每個(gè) dispatch_barrier_async(currentQueue) {}柵欄函數(shù),都是一個(gè)異步函數(shù).執(zhí)行的打印也交叉出現(xiàn).讀取的num值,也并不是我們想要的.出現(xiàn)數(shù)據(jù)亂竄的現(xiàn)象.</pre>

下面是提交到串行化隊(duì)列的代碼:
let currentQueue:dispatch_queue_t = dispatch_queue_create("com.eric", DISPATCH_QUEUE_SERIAL);
    //let currentQueue:dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
    var num = 10
    dispatch_barrier_async(currentQueue) {
        sleep(5)
        num = 11
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 12
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 13
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        num = 14
        print(num)
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("5")
        print(NSThread.currentThread())
    }
    dispatch_barrier_async(currentQueue) {
        print("6")
        print(NSThread.currentThread())
    }
    print("結(jié)束")

輸出結(jié)果:
<pre>結(jié)束
11
<NSThread: 0x15f524d40>{number = 2, name = (null)}
12
<NSThread: 0x15f524d40>{number = 2, name = (null)}
13
<NSThread: 0x15f524d40>{number = 2, name = (null)}
14
<NSThread: 0x15f524d40>{number = 2, name = (null)}
5
<NSThread: 0x15f524d40>{number = 2, name = (null)}
6
<NSThread: 0x15f524d40>{number = 2, name = (null)}</pre>

說(shuō)明:可以看出確實(shí)是順序執(zhí)行的,是串行化隊(duì)列.因?yàn)槲覀兲峤淮a塊的隊(duì)列是一個(gè)串行化的隊(duì)列.按照上文提到執(zhí)行結(jié)果和下面代碼效果一樣

 dispatch_barrier_async(currentQueue) {
        print("6")
        print(NSThread.currentThread())
    }

當(dāng)提交的隊(duì)列是串行隊(duì)列的時(shí)候, 下面的可以替換上面的

dispatch_async(currentQueue) {
        print("6")
        print(NSThread.currentThread())
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末挪凑,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子白热,更是在濱河造成了極大的恐慌冷溃,老刑警劉巖定铜,帶你破解...
    沈念sama閱讀 219,490評(píng)論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件混埠,死亡現(xiàn)場(chǎng)離奇詭異誓酒,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)貌亭,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,581評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門柬唯,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人圃庭,你說(shuō)我怎么就攤上這事锄奢。” “怎么了剧腻?”我有些...
    開封第一講書人閱讀 165,830評(píng)論 0 356
  • 文/不壞的土叔 我叫張陵拘央,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我书在,道長(zhǎng)灰伟,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,957評(píng)論 1 295
  • 正文 為了忘掉前任蕊温,我火速辦了婚禮袱箱,結(jié)果婚禮上遏乔,老公的妹妹穿的比我還像新娘义矛。我一直安慰自己,他們只是感情好盟萨,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,974評(píng)論 6 393
  • 文/花漫 我一把揭開白布凉翻。 她就那樣靜靜地躺著,像睡著了一般捻激。 火紅的嫁衣襯著肌膚如雪制轰。 梳的紋絲不亂的頭發(fā)上前计,一...
    開封第一講書人閱讀 51,754評(píng)論 1 307
  • 那天,我揣著相機(jī)與錄音垃杖,去河邊找鬼男杈。 笑死,一個(gè)胖子當(dāng)著我的面吹牛调俘,可吹牛的內(nèi)容都是我干的伶棒。 我是一名探鬼主播,決...
    沈念sama閱讀 40,464評(píng)論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼彩库,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼肤无!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起骇钦,我...
    開封第一講書人閱讀 39,357評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤宛渐,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后眯搭,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體窥翩,經(jīng)...
    沈念sama閱讀 45,847評(píng)論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,995評(píng)論 3 338
  • 正文 我和宋清朗相戀三年鳞仙,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了鳍烁。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 40,137評(píng)論 1 351
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡繁扎,死狀恐怖幔荒,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情梳玫,我是刑警寧澤爹梁,帶...
    沈念sama閱讀 35,819評(píng)論 5 346
  • 正文 年R本政府宣布,位于F島的核電站提澎,受9級(jí)特大地震影響姚垃,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜盼忌,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,482評(píng)論 3 331
  • 文/蒙蒙 一积糯、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧谦纱,春花似錦看成、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,023評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至,卻和暖如春梦重,著一層夾襖步出監(jiān)牢的瞬間兑燥,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,149評(píng)論 1 272
  • 我被黑心中介騙來(lái)泰國(guó)打工琴拧, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留降瞳,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 48,409評(píng)論 3 373
  • 正文 我出身青樓蚓胸,卻偏偏與公主長(zhǎng)得像力崇,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子赢织,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,086評(píng)論 2 355

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