<!--
{
  "availability" : [
    "iOS: 18.0.0 -",
    "iPadOS: 18.0.0 -",
    "macCatalyst: 18.0.0 -",
    "macOS: 15.0.0 -",
    "tvOS: 18.0.0 -",
    "visionOS: 2.0.0 -",
    "watchOS: 11.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Synchronization/Mutex/withLockIfAvailable(_:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "Synchronization"
    ],
    "preciseIdentifier" : "s:15Synchronization5MutexVAARi_zrlE19withLockIfAvailableyqd__Sgqd__xzYuqd_0_YKYTXEqd_0_YKs5ErrorRd_0_Ri_d__r0_lF"
  },
  "title" : "withLockIfAvailable(_:)"
}
-->

# withLockIfAvailable(_:)

Attempts to acquire the lock and then calls the given closure if
successful.

```
borrowing func withLockIfAvailable<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 if the calling thread acquires
the lock.

## Return Value

The return value, if any, of the `body` closure parameter or
`nil` if the lock couldn’t be acquired.

## Discussion

If the calling thread was successful in acquiring the lock, the
closure will be executed and then immediately after it will
release ownership of the lock. If we were unable to acquire the
lock, this will return `nil`.

This method is equivalent to the following sequence of code:

```
guard mutex.tryLock() else {
  return nil
}
defer {
  mutex.unlock()
}
return try body(&value)
```

> Note: This function cannot spuriously fail to acquire the lock. The
> behavior of similar functions in other languages (such as C’s
> `mtx_trylock()`) is platform-dependent and may differ from Swift’s
> behavior.

---

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)