IOS NSConditionLock condition lock NSCondition asserted

Source: Internet
Author: User

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


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.