---
title: Mutex
framework: synchronization
role: symbol
role_heading: Structure
path: synchronization/mutex
---

# Mutex

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

## Declaration

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

## Overview

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     }   } }

## Topics

### Initializers

- [init(_:)](synchronization/mutex/init(_:).md)

### Instance Methods

- [withLock(_:)](synchronization/mutex/withlock(_:).md)
- [withLockIfAvailable(_:)](synchronization/mutex/withlockifavailable(_:).md)

## Relationships

### Conforms To

- [Sendable](swift/sendable.md)
- [SendableMetatype](swift/sendablemetatype.md)
