I'd prefer to see block-scoped synchronization rather than whole-method-only;
it gives much more flexibility.
Note that you can use the objc synchronization feature with reference types:
// Or (_ obj:...) if you prefer the label-less objc style)
func synchronized(on obj: AnyObject, do block: () throws -> Void) rethrows {
objc_sync_enter(obj)
defer {
objc_sync_exit(obj)
}
try block()
}
// or synchronized(self)
synchronized(on: self) {
// do something
}
try synchronized(on: self) {
throw NSError()
}
Not 100% sure this works with bridged reference types (e.g. Array); definitely
doesn't work with Int. Use a dummy NSObject() if needed.
That said, I'd love to see a swift-native solution that works with value types
(and doesn't rely on the objc runtime).
> On Jun 12, 2017, at 2:10 AM, Erik Aigner via swift-evolution
> <[email protected]> wrote:
> [...]
> With synchronized attribute (the semaphore/wait/deferred-signal is
> synthesized by Swift automatically)
>
> class Obj {
>
> synchronized func method() {
> // semaphore is synthesized automatically, do something…
> }
> }
>
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution