# \`Sychronization.Mutex\<T\>\` in Standard Library, but restricted to iOS 18?

**URL:** <https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803>\
**Category:** Using Swift\
**Created:** [September 20, 2024, 7:30pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803 "2024-09-20T19:30:48Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![EthanLS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ethanls/32/34412_2.png) [@EthanLS](https://forums.swift.org/u/EthanLS)\
**Post date:** [September 20, 2024, 7:30pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/1 "2024-09-20T19:30:48Z")

</div>

I was super thrilled to see `Sychronization.Mutex<T>` in Swift 6: [swift-evolution/proposals/0433-mutex.md at main · swiftlang/swift-evolution · GitHub](https://github.com/swiftlang/swift-evolution/blob/main/proposals/0433-mutex.md)

But for some it can't be used in iOS 17 despite being based on `os_unfair_lock` from iOS 10?

[stdlib/public/Synchronization/Mutex/Mutex.swift](https://github.com/swiftlang/swift/blob/main/stdlib/public/Synchronization/Mutex/Mutex.swift)  
[stdlib/public/Synchronization/Mutex/DarwinImpl.swift](https://github.com/swiftlang/swift/blob/main/stdlib/public/Synchronization/Mutex/DarwinImpl.swift)

But when I "Jump to Definition" in Xcode 16 with iOS 18 SDK I get this signature instead:

```swift
@available(macOS 15.0, iOS 18.0, watchOS 11.0, tvOS 18.0, visionOS 2.0, *)
@frozen public struct Mutex<Value> : ~Copyable where Value : ~Copyable {
...

```

Is there some way to use this on iOS 17?

Apologies if I'm misunderstanding something... I had thought that I could use newer Standard Library features so long as I built against the newer version. Or is it that a static Standard Library version gets added to the OS, and it doesn't matter what I build against?

---

<div class="post-metadata">

**Author:** ![Alejandro](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/alejandro/32/46197_2.png) [@Alejandro](https://forums.swift.org/u/Alejandro)\
**Post date:** [September 20, 2024, 7:35pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/2 "2024-09-20T19:35:55Z")

</div>

New types in the standard library are generally not back deployable, so a new type like `Mutex` regardless of what it's based on, needs to be deployment gated on the first version it appeared. This is an unfortunate limitation right now in Swift.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [September 20, 2024, 7:53pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/3 "2024-09-20T19:53:09Z")

</div>

It would likely be possible to expose the same implementation in a package.

This would be a _polyfill_ rather than back-deployment - the package Mutex wouldn’t be considered the same type as the stdlib Mutex - but since libraries (Apple’s SDKs, the stdlib, any 3rd-party libraries) generally use mutexes as an implementation detail and do not expose them as API, this likely wouldn’t be an issue for most developers.

Maybe we could even make an official polyfill, kind of like we wanted to do with the stdlib preview package way back.

---

<div class="post-metadata">

**Author:** ![EthanLS](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ethanls/32/34412_2.png) [@EthanLS](https://forums.swift.org/u/EthanLS)\
**Post date:** [September 20, 2024, 8:23pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/4 "2024-09-20T20:23:48Z")

</div>

Thanks all for the (obvious) explanations... I'm not sure what I was thinking. 😅

On that note, anyone know how I might replace `Builtin.addressOfRawLayout` to provide a local impl of this same type? 😆

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [September 20, 2024, 8:49pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/5 "2024-09-20T20:49:32Z")

</div>

Can you not already? Builtins are now exposed publicly AFAIK.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 20, 2024, 9:06pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/6 "2024-09-20T21:06:23Z")

</div>

Probably simplest to just separately allocate it instead. The performance loss should be very small unless you have a huge number of locks.

---

<div class="post-metadata">

**Author:** ![jamieQ](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jamieq/32/5117_2.png) [@jamieQ](https://forums.swift.org/u/jamieQ)\
**Post date:** [September 21, 2024, 2:58pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/7 "2024-09-21T14:58:28Z")

</div>

to clarify: what does 'separately allocate it' mean exactly in this context?

---

<div class="post-metadata">

**Author:** ![tshortli](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tshortli/32/15850_2.png) [@tshortli](https://forums.swift.org/u/tshortli)\
**Post date:** [September 21, 2024, 5:23pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/8 "2024-09-21T17:23:09Z")

</div>

I think the suggestion is to use [`OSAllocatedUnfairLock`](https://developer.apple.com/documentation/os/osallocatedunfairlock), which is an existing Darwin platform API that uses the same underlying mutex implementation. The main difference between `Mutex<T>` and `OSAllocatedUnfairLock` is that the latter requires a separate allocation. For the majority of uses of a lock, the separate allocation won't be a significant overhead.

---

<div class="post-metadata">

**Author:** ![David\_Smith](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/david_smith/32/37508_2.png) [@David\_Smith](https://forums.swift.org/u/David_Smith)\
**Post date:** [September 21, 2024, 7:27pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/9 "2024-09-21T19:27:59Z")

</div>

Yup, or if you need to deploy even further back, use the underlying unfair lock directly and use `UnsafePointer.allocate` to make the storage for it. (OSAllocatedUnfairLock is essentially exactly this plus some API refinements to make it harder to misuse in Swift Concurrency contexts)

---

<div class="post-metadata">

**Author:** ![johannesweiss](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/johannesweiss/32/38183_2.png) [@johannesweiss](https://forums.swift.org/u/johannesweiss)\
**Post date:** [September 21, 2024, 8:10pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/10 "2024-09-21T20:10:34Z")

</div>

FWIW, for the time being you could also use (or copy the implementation of) SwiftNIO's [`NIOLockedValueBox<T>`](https://swiftinit.org/docs/swift-nio/nioconcurrencyhelpers/niolockedvaluebox) which is the same thing but has to allocate of course.

---

<div class="post-metadata">

**Author:** ![Hakari](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hakari/32/40650_2.png) [@Hakari](https://forums.swift.org/u/Hakari)\
**Post date:** [April 24, 2025, 3:46pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/11 "2025-04-24T15:46:34Z")

</div>

It seems that the most challenging part of copying the stdlib [Mutex](https://github.com/swiftlang/swift/blob/main/stdlib/public/Synchronization/Mutex/Mutex.swift) implementation locally is the lack of the ability to call `Builtin.addressOfRawLayout` used by the [`_Cell`](https://github.com/swiftlang/swift/blob/main/stdlib/public/Synchronization/Cell.swift) struct.

I've managed to copy the implementation of `Mutex` with a custom `_Cell` struct, can any one tell me how I may improve it or is there any issues with my implementation.

```swift
struct _Cell<Value: ~Copyable>: ~Copyable {
    @usableFromInline
    let _address: UnsafeMutablePointer<Value>

    @_alwaysEmitIntoClient
    @_transparent
    var address: UnsafeMutablePointer<Value> {
        return _address
    }

    @_alwaysEmitIntoClient
    @_transparent
    init(_ initialValue: consuming Value) {
        let address = UnsafeMutablePointer<Value>.allocate(capacity: 1)
        address.initialize(to: initialValue)
        _address = address
    }

    @_alwaysEmitIntoClient
    @inlinable
    deinit {
        _address.deinitialize(count: 1)
        _address.deallocate()
    }
}

```

---

<div class="post-metadata">

**Author:** ![xinnjie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/xinnjie/32/39728_2.png) [@xinnjie](https://forums.swift.org/u/xinnjie)\
**Post date:** [May 23, 2025, 3:05pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/12 "2025-05-23T15:05:32Z")

</div>

Someone already do it, see [GitHub - swhitty/swift-mutex: Cross platform lock backporting the Swift 6 Mutex API to earlier platforms](https://github.com/swhitty/swift-mutex)

---

<div class="post-metadata">

**Author:** ![grynspan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grynspan/32/15315_2.png) [@grynspan](https://forums.swift.org/u/grynspan)\
**Post date:** [May 23, 2025, 11:56pm UTC](https://forums.swift.org/t/sychronization-mutex-t-in-standard-library-but-restricted-to-ios-18/74803/13 "2025-05-23T23:56:00Z")

</div>

> [@David\_Smith](#):
>
> Yup, or if you need to deploy even further back, use the underlying unfair lock directly and use `UnsafePointer.allocate` to make the storage for it.

Swift Testing has to do this because it back-deploys to a macOS/iOS version earlier than `OSAllocatedUnfairLock`. So @EthanLS you can take a look at our implementation if you like. Though, be aware, it's shaped with multi-platform support in mind, so if you only care about Apple platforms then it's probably overcomplicated for your use case.
