<!--
{
  "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",
  "metadataVersion" : "0.1.0",
  "role" : "Structure",
  "symbol" : {
    "kind" : "Structure",
    "modules" : [
      "Synchronization"
    ],
    "preciseIdentifier" : "s:15Synchronization5MutexV"
  },
  "title" : "Mutex"
}
-->

# Mutex

A synchronization primitive that protects shared mutable state via
mutual exclusion.

```
@frozen struct Mutex<Value> where Value : ~Copyable
```

## Overview

The `Mutex` type offers non-recursive exclusive access to the state
it is protecting by blocking threads attempting to acquire the lock.
Only one execution context at a time has access to the value stored
within the `Mutex` allowing for exclusive access.

An example use of `Mutex` in a class used simultaneously by many
threads protecting a `Dictionary` value:

```
class Manager {
  let cache = Mutex<[Key: Resource]>([:])

  func saveResource(_ resource: Resource, as key: Key) {
    cache.withLock {
      $0[key] = resource
    }
  }
}
```

---

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)