Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | NSLock.h |
Related sample code |
The NSConditionLock
class defines objects whose locks can be associated with specific, user-defined conditions. Using an NSConditionLock
object, you can ensure that a thread can acquire a lock only if a certain condition is met. Once it has acquired the lock and executed the critical section of code, the thread can relinquish the lock and set the associated condition to something new. The conditions themselves are arbitrary: you define them as needed for your application.
– lockBeforeDate:
– lockWhenCondition:
– lockWhenCondition:beforeDate:
– tryLock
– tryLockWhenCondition:
– unlockWithCondition:
Returns the condition associated with the receiver.
- (NSInteger)condition
The condition associated with the receiver. If no condition has been set, returns 0.
NSLock.h
Initializes a newly allocated NSConditionLock
object and sets its condition.
- (id)initWithCondition:(NSInteger)condition
The user-defined condition for the lock. The value of condition is user-defined; see the class description for more information.
An initialized condition lock object; may be different than the original receiver.
NSLock.h
Attempts to acquire a lock before a specified moment in time.
- (BOOL)lockBeforeDate:(NSDate *)limit
The date by which the lock must be acquired or the attempt will time out.
YES
if the lock is acquired within the time limit, NO
otherwise.
The condition associated with the receiver isn’t taken into account in this operation. This method blocks the thread’s execution until the receiver acquires the lock or limit is reached.
NSLock.h
Attempts to acquire a lock.
- (void)lockWhenCondition:(NSInteger)condition
The condition to match on.
The receiver’s condition must be equal to condition before the locking operation will succeed. This method blocks the thread’s execution until the lock can be acquired.
NSLock.h
Attempts to acquire a lock before a specified moment in time.
- (BOOL)lockWhenCondition:(NSInteger)condition beforeDate:(NSDate *)limit
The condition to match on.
The date by which the lock must be acquired or the attempt will time out.
YES
if the lock is acquired within the time limit, NO
otherwise.
The receiver’s condition must be equal to condition before the locking operation will succeed. This method blocks the thread’s execution until the lock can be acquired or limit is reached.
NSLock.h
Returns the name associated with the receiver.
- (NSString *)name
The name of the receiver.
NSLock.h
Assigns a name to the receiver.
- (void)setName:(NSString *)newName
The new name for the receiver. This method makes a copy of the specified string.
You can use a name string to identify a condition lock within your code. Cocoa also uses this name as part of any error descriptions involving the receiver.
NSLock.h
Attempts to acquire a lock without regard to the receiver’s condition.
- (BOOL)tryLock
YES
if the lock could be acquired, NO
otherwise.
This method returns immediately.
NSLock.h
Attempts to acquire a lock if the receiver’s condition is equal to the specified condition.
- (BOOL)tryLockWhenCondition:(NSInteger)condition
YES
if the lock could be acquired, NO
otherwise.
As part of its implementation, this method invokes lockWhenCondition:beforeDate:
. This method returns immediately.
NSLock.h
Relinquishes the lock and sets the receiver’s condition.
- (void)unlockWithCondition:(NSInteger)condition
The user-defined condition for the lock. The value of condition is user-defined; see the class description for more information.
NSLock.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-01-15)