Inherits from | |
Conforms to | |
Framework | /System/Library/Frameworks/Foundation.framework |
Availability | Available in Mac OS X v10.0 and later. |
Companion guide | |
Declared in | 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
object 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.
Returns an NSDistributedLock
object initialized to use as the locking object the file-system entry specified by a given path.
+ (NSDistributedLock *)lockWithPath:(NSString *)aPath
All of aPath up to the last component itself must exist. You can use NSFileManager
to create (and set permissions) for any nonexistent intermediate directories.
An NSDistributedLock
object initialized to use as the locking object the file-system entry specified by aPath.
For applications to use the lock, aPath must be accessible to—and writable by—all hosts on which the applications might be running.
NSDistributedLock.h
Forces the lock to be relinquished.
- (void)breakLock
This method always succeeds unless the lock has been damaged. If another process has already unlocked or broken the lock, this method has no effect. You should generally use unlock
rather than breakLock
to relinquish a lock.
Warning:
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 an NSGenericException
if the lock could not be removed.
NSDistributedLock.h
Initializes an NSDistributedLock
object to use as the lock the file-system entry specified by a given path.
- (id)initWithPath:(NSString *)aPath
All of aPath up to the last component itself must exist. You can use NSFileManager
to create (and set permissions) for any nonexistent intermediate directories.
An NSDistributedLock
object initialized to use as the locking object the file-system entry specified by aPath.
For applications to use the lock, aPath must be accessible to—and writable by—all hosts on which the applications might be running.
NSDistributedLock.h
Returns the time the receiver was acquired by any of the NSDistributedLock
objects using the same path.
- (NSDate *)lockDate
The time the receiver was acquired by any of the NSDistributedLock
objects using the same path. Returns nil
if the lock doesn’t exist.
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.
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.
NSDistributedLock.h
Attempts to acquire the receiver and immediately returns a Boolean value that indicates whether the attempt was successful.
- (BOOL)tryLock
YES
if the attempt to acquire the receiver was successful, otherwise NO
.
Raises NSGenericException
if a file-system error occurs.
NSDistributedLock.h
Relinquishes the receiver.
- (void)unlock
You should generally use the unlock
method rather than breakLock
to release a lock.
An NSGenericException
is raised if the receiver doesn’t already exist.
NSDistributedLock.h
© 2007 Apple Inc. All Rights Reserved. (Last updated: 2007-01-22)