關于dispatch_barrier_async和dispatch_barrier_sync

在iOS多線程中,我們可以用GCD的串行隊列來實現同步鎖的效果。通過在把任務添加到串行隊列中來依次執行,達到同步的效果。
同時GCD又提供了兩個函數,來方便我們實現這個同步效果。他們就是dispatch_barrier_asyncdispatch_barrier_sync

先看官方文檔


Function dispatch_barrier_async


Submits a barrier block for asynchronous execution and returns immediately.
提交一個異步執行且立即返回的柵欄block

Declaration

void dispatch_barrier_async(dispatch_queue_t queue, dispatch_block_t block);

Parameters

The dispatch queue on which to execute the barrier block. The queue is retained by the system until the block has run to completion. This parameter cannot be NULL.
執行柵欄block的調度隊列。系統會持有這個隊列直到這個block執行完成。這個參數不能為NULL

The barrier block to submit to the target dispatch queue. This block is copied and retained until it finishes executing, at which point it is released. This parameter cannot be NULL.
提交到目標調度隊列的柵欄block。block會被拷貝和持有直到它執行完成,完成后會被釋放。這個參數不能為NULL

Discussion

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. Instead, the queue waits until its currently executing blocks finish executing. At that point, the barrier block executes by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes.

當這個block被提交到隊列后就立即返回,并不會等待這個block的調用。當柵欄block到達指定的并發隊列的時候,這個block不會立即執行。相反,這個隊列會等待當前正在執行的blocks完成執行。這時,柵欄block自行執行。一些后面提交的blocks會等到柵欄block執行完成之后才會開始執行。

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.

指定的queue應該是你通過函數 dispatch_queue_create創建的并發隊列。如果你傳入一個串行隊列或者全局并發隊列。這個方法就像dispatch_async 一樣。


Function dispatch_barrier_sync


Submits a barrier block object for execution and waits until that block completes.
提交一個柵欄block執行,等待這個block執行完成。

Declaration

void dispatch_barrier_sync(dispatch_queue_t queue, dispatch_block_t block);

Parameters

The dispatch queue on which to execute the barrier block. This parameter cannot be NULL.
執行柵欄block的指定隊列。參數不能為NULL

The barrier block to be executed. This parameter cannot be NULL.
執行的柵欄block。參數不能為NULL

Discussion

Submits a barrier block to a dispatch queue for synchronous execution. Unlike dispatch_barrier_async, this function does not return until the barrier block has finished. Calling this function and targeting the current queue results in deadlock.
提交一個同步執行的柵欄block到指定隊列。不像dispatch_barrier_async,這個方法直到柵欄block執行完成后才會返回。調用這個函數在當前線程會導致死鎖。
注:隊列引起的循環等待導致死鎖

When the barrier block reaches the front of a private concurrent queue, it is not executed immediately. Instead, the queue waits until its currently executing blocks finish executing. At that point, the queue executes the barrier block by itself. Any blocks submitted after the barrier block are not executed until the barrier block completes.
當這個block被提交到隊列后就立即返回,并不會等待這個block的調用。當柵欄block到達指定的并發隊列的時候,這個block不會立即執行。相反,這個隊列會等待當前正在執行的blocks完成執行。這時,柵欄block自行執行。一些后面提交的blocks會等到柵欄block執行完成之后才會開始執行

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_sync function.
指定的queue應該是你通過函數 dispatch_queue_create創建的并發隊列。如果你傳入一個串行隊列或者全局并發隊列。這個方法就像dispatch_sync 一樣。

Unlike with dispatch_barrier_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

不像 dispatch_barrier_async,目標隊列上不會執行retain操作。因為調用這個函數是同步,他不會被調用者“借用”。此外,對這個block不會執行Block_copy

As an optimization, this function invokes the barrier block on the current thread when possible.
為了優化,這個函數應該盡可能的調用當前線程的柵欄block。

其實把整個文檔一翻譯完,我想要說的就已經全部包括了,所以遇到問題,先看官方的文檔是不錯的選擇。

另外我想說一下在iOS中用得比較多的一個場景,多讀單寫。

對于一個對象中的某個屬性,我們可以自定義兩個方法來實現對其的多讀單寫操作。

-(NSUInteger)ticketCount {
    __block NSUInteger count;
    //因為要立即返回,所以我們用dispatch_sync
    dispatch_sync(_concurrent_queue, ^{
        count = self->_ticketCount;
    });
    return count;
}

- (void)setTicketCount:(NSUInteger)ticketCount {
    //對于寫操作,我們這里用dispatch_barrier_async,用柵欄函數實現線程安全
    dispatch_barrier_async(_concurrent_queue, ^{
        if (ticketCount != self->_ticketCount) {
            self->_ticketCount = ticketCount;
        }
    });
}

其實一般我們不會通過屬性的getter和setter方法來寫多讀單寫,一般會自己定義兩個方法,在這兩個方法中,不一定僅僅是簡單的讀寫,也可以定義一些自己的邏輯。例如我們賣火車票的一個例子。


-(NSUInteger)ticketCount {
   __block NSUInteger count;
   dispatch_sync(_concurrent_queue, ^{
       count = self->_ticketCount;
   });
   return count;
}
-(NSInteger)sellTicketCount:(NSUInteger)count withUserId:(NSUInteger)uid {
   __block NSInteger sellCount;
   dispatch_barrier_sync(_concurrent_queue, ^{
       if ([self queryUserHaveBoughtWithUserId:uid]) {
           //是否購買過
           sellCount = -1; //-1代表已經買過票了
       } else {
           if (count > self->_ticketCount) {
               sellCount = self->_ticketCount;
               self->_ticketCount = 0;
           } else {
               sellCount = count;
               self->_ticketCount = self->_ticketCount - count;
           }

           BoughtUser *buyer = [[BoughtUser alloc] init];
           buyer.uid = uid;
           buyer.count = sellCount;
           [self->_haveBoughtUsers addObject:buyer];
       }
   });
   return sellCount;
}

- (BOOL)refundTicketCount:(NSUInteger)count withUserId:(NSUInteger)uid {
   __block BOOL isSuccess = NO;
   dispatch_barrier_sync(_concurrent_queue, ^{
       if ([self queryUserBoughtCountWithUserId:uid] == count) {
           //只能退還全部車票
           self->_ticketCount = self->_ticketCount + count;
           isSuccess = YES;
       }
   });
   return isSuccess;
}

這也算一個多讀單寫的具體實現。這里因為要返回購買后剩余的票數,所以只能用dispatch_barrier_sync函數。

需要注意的一定要傳入自己創建的并發隊列

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 229,908評論 6 541
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,324評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,018評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,675評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,417評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,783評論 1 329
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,779評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,960評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,522評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,267評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,471評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,009評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,698評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,099評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,386評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,204評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,436評論 2 378

推薦閱讀更多精彩內容

  • Execute code concurrently on multicore hardware by submit...
    ngugg閱讀 601評論 0 1
  • 簡介 在iOS中,我們需要將非UI且耗時的任務放在主線程當中執行,同時確保在任務完成時進行回調。常用的三種實現多線...
    adduct閱讀 390評論 0 1
  • 一:base.h 二:block.h 1. dispatch_block_flags:DISPATCH_BLOCK...
    小暖風閱讀 2,476評論 0 0
  • NSThread 第一種:通過NSThread的對象方法 NSThread *thread = [[NSThrea...
    攻城獅GG閱讀 828評論 0 3
  • 2018年10月8日 星期一 晴 假期結束,打個電話。 卻聽電話那頭的聲音慵懶,好像剛被鈴聲叫醒。 呀呀,假日...
    也涼閱讀 112評論 0 3