---
title: "withLock(_:)"
framework: synchronization
role: symbol
role_heading: Instance Method
path: "synchronization/mutex/withlock(_:)"
---

# withLock(_:)

Calls the given closure after acquiring the lock and then releases ownership.

## Declaration

```swift
borrowing func withLock<Result, E>(_ body: (inout sending Value) throws(E) -> sending Result) throws(E) -> sending Result where E : Error, Result : ~Copyable
```

## Parameters

- `body`: A closure with a parameter of Value that has exclusive access to the value being stored within this mutex. This closure is considered the critical section as it will only be executed once the calling thread has acquired the lock.

## Return Value

Return Value The return value, if any, of the body closure parameter.

## Discussion

Discussion This method is equivalent to the following sequence of code: mutex.lock() defer {   mutex.unlock() } return try body(&value) warning: Recursive calls to withLock within the closure parameter has behavior that is platform dependent. Some platforms may choose to panic the process, deadlock, or leave this behavior unspecified. This will never reacquire the lock however.
