A lock that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file.
SDK
- macOS 10.0+
Framework
- Foundation
Declaration
class NSDistributedLock : NSObject
Overview
The lock is implemented by an entry (such as a file or directory) in the file system. For multiple applications to use an NSDistributed
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 try()
method to attempt to acquire a lock. You should generally use the unlock()
method to release the lock rather than break()
.
NSDistributed
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 NSDistributed
object, this could mean polling the file system at some predetermined rate. A better solution is to provide the try()
method and let you determine the polling frequency that makes sense for your application.