你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS NSConditionLock 條件鎖 NSCondition 斷言

IOS NSConditionLock 條件鎖 NSCondition 斷言

編輯:關於IOS

NSConditionLock 條件鎖 NSCondition 斷言
NSConditionLock 繼承類NSObject 遵從的協議NSLocking NSObject

NSConditionLock 類定義特定的、用戶定義的條件鎖

協議NSLocking 方法 lock unlock

//初始化一個NSConditionLock對象
- (id)initWithCondition:(NSInteger)condition

//返回一個Condition
- (NSInteger)condition

//獲取和釋放鎖
1、C (BOOL)lockBeforeDate:(NSDate *)limit
//在指定時間前嘗試獲取鎖,若成功則返回YES 否則返回NO
2、C (void)lockWhenCondition:(NSInteger)condition

//嘗試獲取鎖。在加鎖成功前接收對象的Condition必須與參數Condition 相同

3、C (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit
//同上,只是又加上了一個時間

4、C (BOOL)tryLock //嘗試著獲取鎖
5、C (BOOL)tryLockWhenCondition:(NSInteger)condition
//如果接收對象的condition與給定的condition相等,則嘗試獲取鎖
6、C (void)unlockWithCondition:(NSInteger)condition
//解鎖並設置接收對象的condition

//訪問方法
C setName:
C name

NSCondition 類 斷言

用一個NSCondition類對象的步驟
1、Lock the condition object.
2、Test a boolean predicate. (This predicate is a boolean flag or other variable in your code that indicates whether it is safe to perform the task protected by the condition.)
3、If the boolean predicate is false, call the condition object’s wait or waitUntilDate: method to block the thread. Upon returning from these methods, go to step 2 to retest your boolean predicate. (Continue waiting and retesting the predicate until it is true.)
4、If the boolean predicate is true, perform the task.
5、Optionally update any predicates (or signal any conditions) affected by your task.
6、When your task is done, unlock the condition object.
英語不太好也就不翻譯了,大概意思都能看懂

下面看一下方法
//使線程等待
1、- (void)wait //阻塞當前線程,直到收到信號
2、- (BOOL)waitUntilDate:(NSDate *)limit
//阻塞當前線程,直到收到信號或到達指定的時間

///發信號給阻塞的線程
1、- (void)signal //喚醒一個阻塞的線程
2、- (void)broadcast //喚醒所有阻塞的線程

///訪問方法
C setName:

C name

自己看官方文檔是的總結 ―― LC

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved