在iOS多線程中,我們可以用GCD的串行隊列來實現同步鎖的效果。通過在把任務添加到串行隊列中來依次執行,達到同步的效果。
同時GCD又提供了兩個函數,來方便我們實現這個同步效果。他們就是dispatch_barrier_async 和 dispatch_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函數。
需要注意的一定要傳入自己創建的并發隊列