NSConditionLock condition lock NSCondition asserted
NSConditionLock inherits NSObject-compliant protocols NSLocking NSObject
The NSConditionLock class defines specific and user-defined conditional locks.
Protocol NSLocking method lock unlock
// Initialize an NSConditionLock object
-(Id) initWithCondition :( NSInteger) condition
// Returns a Condition.
-(NSInteger) condition
// Obtain and release the lock
1.-(BOOL) lockBeforeDate :( NSDate *) limit
// Try to obtain the lock before the specified time. If the lock succeeds, YES is returned. Otherwise, NO is returned.
2.-(void) lockWhenCondition :( NSInteger) condition
// Try to obtain the lock. The Condition of the received object must be the same as the Condition parameter before the lock is successfully applied.
3.-(BOOL) lockWhenCondition :( NSInteger) condition beforeDate :( NSDate *) limit
// Same as above, but adds another time
4.-(BOOL) tryLock // try to get the lock
5.-(BOOL) tryLockWhenCondition :( NSInteger) condition
// If the condition of the receiving object is equal to the given condition, the system attempts to obtain the lock.
6.-(void) unlockWithCondition :( NSInteger) condition
// Unlock and set the condition of the recipient
// Access method
-SetName:
-Name
NSCondition assertions
Steps for using an NSCondition Class Object
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.
If your English is not very good, you will not be able to understand it.
Let's take a look at the method below
// Wait the thread
1.-(void) wait // block the current thread until the signal is received
2.-(BOOL) waitUntilDate :( NSDate *) limit
// Block the current thread until the signal is received or the specified time is reached
/// Send a signal to the blocked thread
1.-(void) signal // wake up a blocked thread
2.-(void) broadcast // wake up all blocked threads
/// Access method
-SetName:
-Name
Read the official document by yourself. Summary: LC