- Inherits from:
- NSObject
- Conforms to:
- NSObject
- (NSObject)
Declared in:
- Foundation/NSDistributedLock.h
The NSDistributedLock class defines an object that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file.
The lock is implemented by an entry (such as a file or directory) in the file system. For multiple applications to use an NSDistributedLock to coordinate their activities, the lock must be writable on a file system accessible to all hosts on which the applications might be running.
Use the tryLock method to attempt to acquire a lock. You should generally use the unlock method to release the lock rather than breakLock.
NSDistributedLock doesn't conform to the NSLocking protocol nor does it have a lock method. The protocol's lock method is intended to block the execution of the thread until successful. For an NSDistributedLock object, this could mean polling the file system at some predetermined rate. A better solution is to provide the tryLock method and let you determine the polling frequency that makes sense for your application.
- Creating an NSDistributedLock
- + lockWithPath:
- - initWithPath:
- Acquiring a lock
- - tryLock
- Relinquishing a lock
- - breakLock
- - unlock
- Getting lock information
- - lockDate
+ (NSDistributedLock *)lockWithPath:(NSString
*)aPath
All of aPath up to the last component itself must exist. Use NSFileManager to create (and set permissions for) any nonexistent intermediate directories.
See Also: - initWithPath:
- (void)breakLock
Because breakLock
can
release another process's lock, it should be used with great caution.
Even if you break a lock, there's no guarantee that you will then be able to acquire the lock-another process might get it before your tryLock is invoked.
Raises NSGenericException if the lock could not be removed.
See Also: - unlock
- (NSDistributedLock *)initWithPath:(NSString
*)aPath
All of aPath up to the last component itself must exist. Use NSFileManager to create (and set permissions) for any nonexistent intermediate directories.
See Also: + lockWithPath:
- (NSDate *)lockDate
This method is potentially useful to applications that want to use an age heuristic to decide if a lock is too old and should be broken. Returns nil if the lock doesn't exist.
If the creation date on the lock isn't the date on which you locked it, you've lost the lock: It's been broken since you last checked it.
- (BOOL)tryLock
Raises NSGenericException if a file system error occurs.
See Also: - unlock
- (void)unlock
An NSGenericException is raised if the lock doesn't already exist.
See Also: - breakLock