iOSロックシリーズ-NSConditionLock条件付きロック



Ios Lock Series Nsconditionlock Conditional Lock



@interface NSConditionLock : NSObject { @private void *_priv } / / Initialize an NSConditionLock object - (instancetype)initWithCondition:(NSInteger)condition NS_DESIGNATED_INITIALIZER @property (readonly) NSInteger condition //lock condition / / Lock when the condition is met - (void)lockWhenCondition:(NSInteger)condition - (BOOL)tryLock / / If the condition of the receiving object is equal to the given condition, then try to acquire the lock, do not block the thread - (BOOL)tryLockWhenCondition:(NSInteger)condition //Unlock, reset the lock condition - (void)unlockWithCondition:(NSInteger)condition - (BOOL)lockBeforeDate:(NSDate *)limit / / Try to acquire the lock before the specified time, if it succeeds, return YES or return NO - (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit @property (nullable, copy) NSString *name API_AVAILABLE(macos(10.5), ios(2.0), watchos(2.0), tvos(9.0)) @end

コード例:

//Main thread NSConditionLock *lock = [[NSConditionLock alloc] initWithCondition:0] //Thread 1 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [lock lockWhenCondition:1] NSLog (@'thread 1') sleep(2) [lock unlock] }) //Thread 2 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ Sleep(1)//to ensure that the thread 2 code is executed afterwards if ([lock tryLockWhenCondition:0]) { NSLog(@'thread 2') [lock unlockWithCondition:2] NSLog (@'Thread 2 unlocked successfully') } else { NSLog (@'Thread 2 attempted to lock failed') } }) //Thread 3 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // sleep(2)// to ensure that the thread 2 code is executed afterwards if ([lock tryLockWhenCondition:2]) { NSLog (@'thread 3') [lock unlock] NSLog (@'Thread 3 unlocked successfully') } else { NSLog (@'Thread 3 tried to lock failed') } }) //Thread 4 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // sleep(3)// to ensure that the thread 2 code is executed afterwards [lock lockWhenCondition:2] NSLog(@'thread 4') [lock unlockWithCondition:1] NSLog (@'Thread 4 unlocked successfully') })

結果を印刷します。



2018-04-17 16:59:26.995255+0800 NSLock test [48379:5945905] Thread 3 failed to lock 2018-04-17 16:59:28.000120+0800 NSLock test [48379:5945903] Thread 2 2018-04-17 16:59:28.000791+0800 NSLock test [48379:5945903] Thread 2 unlocked successfully 2018-04-17 16:59:28.000851+0800 NSLock test [48379:5945904] Thread 4 2018-04-17 16:59:28.001999+0800 NSLock test [48379:5945904] Thread 4 unlocked successfully 2018-04-17 16:59:28.002044+0800 NSLock test [48379:5945902] Thread 1

結果は次のとおりです。

10の条件で条件付きロックを初期化します
2スレッド1とスレッド4の条件が満たされていないため、ループはしばらくスリープ状態になり、条件が満たされたときにウェイクアップするのを待ちます。スレッド3はロックを試み、スレッドをブロックしませんが、条件は満たされません。ダイレクトスリープスレッド2は1秒間スリープしてから、ロックを試みます。条件が満たされているため、ロックは成功しています
3スレッド2はリセットロック条件2でロック解除されます
4この時点で、スレッド4は条件を満たし、システムはウェイクアップしてロックし、ロック条件をリセットします14
5この時点で、スレッド1は条件を満たし、システムはウェイクアップしてロックし、ロックを解除します。条件は1です。