<!--
{
  "availability" : [
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSDistributedLock",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSDistributedLock"
  },
  "title" : "NSDistributedLock"
}
-->

# NSDistributedLock

A lock that multiple applications on multiple hosts can use to restrict access to some shared resource, such as a file.

```
class NSDistributedLock
```

## Overview

The lock is implemented by an entry (such as a file or directory) in the file system. For multiple applications to use an [`NSDistributedLock`](/documentation/Foundation/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 [`try()`](/documentation/Foundation/NSDistributedLock/try()) method to attempt to acquire a lock. You should generally use the [`unlock()`](/documentation/Foundation/NSDistributedLock/unlock()) method to release the lock rather than [`break()`](/documentation/Foundation/NSDistributedLock/break()).

[`NSDistributedLock`](/documentation/Foundation/NSDistributedLock) doesn’t conform to the [`NSLocking`](/documentation/Foundation/NSLocking) protocol, nor does it have a `lock` method. The protocol’s [`lock()`](/documentation/Foundation/NSLocking/lock()) method is intended to block the execution of the thread until successful. For an [`NSDistributedLock`](/documentation/Foundation/NSDistributedLock) object, this could mean polling the file system at some predetermined rate. A better solution is to provide the [`try()`](/documentation/Foundation/NSDistributedLock/try()) method and let you determine the polling frequency that makes sense for your application.

## Topics

### Creating an NSDistributedLock

[`lockWithPath:`](/documentation/Foundation/NSDistributedLock/lockWithPath:)

Returns an `NSDistributedLock` object initialized to use as the locking object the file-system entry specified by a given path.

[`init(path:)`](/documentation/Foundation/NSDistributedLock/init(path:))

Initializes an `NSDistributedLock` object to use as the lock the file-system entry specified by a given path.

### Acquiring a Lock

[`try()`](/documentation/Foundation/NSDistributedLock/try())

Attempts to acquire the receiver and immediately returns a Boolean value that indicates whether the attempt was successful.

### Relinquishing a Lock

[`break()`](/documentation/Foundation/NSDistributedLock/break())

Forces the lock to be relinquished.

[`unlock()`](/documentation/Foundation/NSDistributedLock/unlock())

Relinquishes the receiver.

### Getting Lock Information

[`lockDate`](/documentation/Foundation/NSDistributedLock/lockDate)

Returns the time the receiver was acquired by any of the `NSDistributedLock` objects using the same path.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)