O'Reilly logo

Cocoa in a Nutshell by James Duncan Davidson, Michael Beam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

NSDistributedLock — Mac OS X 10.0

Synopsis

This class provides an interface to an object that can be used by multiple applications to control access to a shared resource (such as a file). This class works by creating an entry in the filesystem at a path known to each application that is interested in acquiring a lock to access the shared resource.

A distributed lock object is initialized with a filesystem entry using the method initWithPath:. To acquire a lock, thus making it safe to access a resource, we use the method tryLock. This method returns a BOOL indicating whether or not it was successful in attempting to acquire a lock. To relinquish a previously acquired lock, use the method unlock.

image with no caption

@interface NSDistributedLock : NSObject
                                  // Initializers
   - (id)initWithPath:(NSString *)path;
                                  // Class Methods
   + (NSDistributedLock *)lockWithPath:(NSString *)path;
                                  // Instance Methods
   - (void)breakLock;
   - (NSDate *)lockDate;
   - (BOOL)tryLock;
   - (void)unlock;

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required