---
title: lock()
framework: foundation
role: symbol
role_heading: Instance Method
path: foundation/nslocking/lock()
---

# lock()

Attempts to acquire a lock, blocking a thread’s execution until the lock can be acquired.

## Declaration

```swift
func lock()
```

## Discussion

Discussion An application protects a critical section of code by requiring a thread to acquire a lock before executing the code. Once the critical section is completed, the thread relinquishes the lock by invoking unlock().

## See Also

### Related Documentation

- [Threading Programming Guide](apple-archive/documentation/Cocoa/Conceptual/Multithreading/Introduction.md)

### Working with Locks

- [unlock()](foundation/nslocking/unlock().md)
