Avatar
swift-async-algorithmsにManagedCriticalStateというstructがあり、 https://github.com/apple/swift-async-algorithms/blob/main/Sources/AsyncAlgorithms/Locking.swift 用途としては、複数のactorとかqueueで状態を共有する場合や、前に他のスレッドで話にあったwithTaskCancellationHandlerのcancelHandlerで利用することが想定されているのですが、 https://github.com/apple/swift-async-algorithms/pull/7 actor内部の値にsyncにアクセスしたい場合にも使えるのかなと思ったのですが、どうなんでしょう?withCriticalRegion内部でawaitできないのでデータ競合は起きないのかなーと思ったのですが… actor A { let state = ManagedCriticalState(0) nonisolated var value: Int { state.withCriticalRegion { $0 } } func update() { state.withCriticalRegion { $0 += 1 } } } func test() async { let a = A() await a.update() print(a.value) // 1 } await test()
Async Algorithms for Swift. Contribute to apple/swift-async-algorithms development by creating an account on GitHub.
This is a pre-requisite for managing state for backing storage that cannot be managed as an actor (due to cancellation). Locking is heavily inspired from the cross platform implementation in swift-...