Comments (3)
Since the argument passed in this method is inout
, the language prevents you from calling it simultaneously from multiple threads by crashing. See https://swift.org/blog/swift-5-exclusivity/.
from opencombine.
I think it shouldn't, most of the time you don't need it so synchronization should be implemented only for specific cases as needed...
from opencombine.
@bofeizhu you may use something like that
import Foundation
@propertyWrapper
public struct ThreadSafeWithLock<Value> {
private var value: Value
private let lock: NSLocking
public var locksOnRead: Bool = false
public init(
wrappedValue: Value,
_ lock: NSLocking = NSLock(),
locksOnRead: Bool = false
) {
self.value = wrappedValue
self.lock = lock
self.locksOnRead = locksOnRead
}
public var wrappedValue: Value {
get {
if locksOnRead {
return lock.execute { value }
} else {
return value
}
}
set { lock.store(newValue, in: &value) }
}
}
@ThreadSafeWithLock
var subscriptions: Set<AnyCancellable> = []
publisher
.sink { ... }
.store(in &subscriptions)
import Foundation
extension NSLocking {
/// Atomically stores new value in object
@inlinable
public func store<T>(_ value: T, in object: inout T) {
mutate(&object, with: { $0 = value })
}
/// Atomically mutates object with closure
@inlinable
public func mutate<T: AnyObject>(_ object: T, with closure: (T) -> Void) {
execute { closure(object) }
}
/// Atomically mutates object with closure
@inlinable
public func mutate<T>(_ object: inout T, with closure: (inout T) -> Void) {
execute { closure(&object) }
}
/// Atomically mutates object with closure
@inlinable
public func set<T: AnyObject, Value>(
_ object: T,
_ keyPath: ReferenceWritableKeyPath<T, Value>,
_ value: Value
) {
execute { object[keyPath: keyPath] = value }
}
/// Atomically mutates object with closure
@inlinable
public func set<T, Value>(_ object: inout T, _ keyPath: WritableKeyPath<T, Value>, _ value: Value)
{
execute { object[keyPath: keyPath] = value }
}
/// Atomically executes a block of code
@discardableResult
@inlinable
public func execute<T>(code closure: () -> T) -> T {
lock()
defer { unlock() }
return closure()
}
}
from opencombine.
Related Issues (20)
- the `*` in DispatchQueue scheduler's SchedulerTimeType gives wrong value HOT 3
- Ready for production? HOT 3
- [Consideration] Bridge from OpenCombine to Combine publishers HOT 12
- Slack Invite is no longer valid HOT 1
- Doesn't compile on macOS Monterey / XCode 13 HOT 2
- [Feature Request] Async Publisher.values HOT 1
- Re-enable concurrency tests on Wasm when swiftwasm supports them
- Re-enable concurrency tests on Windows when Windows issues are fixed
- error HOT 5
- [Bug]The Publishers.ReceiveOn may lead to subscriber never receive the published single when schedule is concurrent queue.
- [Bug]The Publishers.ReceiveOn may lead to subscriber never receive the published signal when scheduler is concurrent queue.
- would be nice to be able to use "@Published" directive in earlier Swift versions that do not know it HOT 2
- Add primary associated type support HOT 1
- Unable to build on Ubuntu 22.04.1 HOT 2
- Zip crash HOT 3
- When BUILD_LIBRARY_FOR_DISTRIBUTION is set to YES, the build will fail. HOT 1
- Xcode 15 crash HOT 1
- EXC_BAD_ACCESS when repeatedly using Future HOT 3
- When Future call Cancel, why parent is copied by take()?
- error build: Undefined symbol: _OBJC_CLASS_$
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from opencombine.