<!--
{
  "availability" : [
    "iOS: 2.0.0 -",
    "iPadOS: 2.0.0 -",
    "macCatalyst: 13.0.0 -",
    "macOS: 10.0.0 -",
    "tvOS: 9.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 2.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Foundation",
  "identifier" : "/documentation/Foundation/NSConditionLock",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "Foundation"
    ],
    "preciseIdentifier" : "c:objc(cs)NSConditionLock"
  },
  "title" : "NSConditionLock"
}
-->

# NSConditionLock

A lock that can be associated with specific, user-defined conditions.

```
class NSConditionLock
```

## Overview

Using an [`NSConditionLock`](/documentation/Foundation/NSConditionLock) object, you can ensure that a thread can acquire a lock only if a certain condition is met. Once it has acquired the lock and executed the critical section of code, the thread can relinquish the lock and set the associated condition to something new. The conditions themselves are arbitrary: you define them as needed for your application.

## Topics

### Initializing an NSConditionLock Object

[`-  initWithCondition:`](/documentation/Foundation/NSConditionLock/init(condition:))

Initializes a newly allocated `NSConditionLock` object and sets its condition.

### Accessing the Condition

[`condition`](/documentation/Foundation/NSConditionLock/condition)

The condition associated with the receiver.

### Acquiring and Releasing a Lock

[`-  lockBeforeDate:`](/documentation/Foundation/NSConditionLock/lock(before:))

Attempts to acquire a lock before a specified moment in time.

[`-  lockWhenCondition:`](/documentation/Foundation/NSConditionLock/lock(whenCondition:))

Attempts to acquire a lock.

[`-  lockWhenCondition:beforeDate:`](/documentation/Foundation/NSConditionLock/lock(whenCondition:before:))

Attempts to acquire a lock before a specified moment in time.

[`-  tryLock`](/documentation/Foundation/NSConditionLock/try())

Attempts to acquire a lock without regard to the receiver’s condition.

[`-  tryLockWhenCondition:`](/documentation/Foundation/NSConditionLock/tryLock(whenCondition:))

Attempts to acquire a lock if the receiver’s condition is equal to the specified condition.

[`-  unlockWithCondition:`](/documentation/Foundation/NSConditionLock/unlock(withCondition:))

Relinquishes the lock and sets the receiver’s condition.

### Identifying the Condition Lock

[`name`](/documentation/Foundation/NSConditionLock/name)

The name associated with the receiver.

## Relationships

### Conforms To

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

[`Equatable`](/documentation/Swift/Equatable)

[`Sendable`](/documentation/Swift/Sendable)

[`NSLocking`](/documentation/Foundation/NSLocking)

[`Hashable`](/documentation/Swift/Hashable)

[`CustomStringConvertible`](/documentation/Swift/CustomStringConvertible)

[`CVarArg`](/documentation/Swift/CVarArg)

[`NSObjectProtocol`](/documentation/ObjectiveC/NSObjectProtocol)

[`CustomDebugStringConvertible`](/documentation/Swift/CustomDebugStringConvertible)

### Inherits From

[`NSObject-swift.class`](/documentation/ObjectiveC/NSObject-swift.class)

---

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)