[swift-evolution] Introducing synthesized locks

Erik Aigner erik at bytieful.com
Mon Jun 12 04:10:08 CDT 2017

In my day to day tasks, synchronization primitives are used quite often. ObjC had the @synchronized attribute for methods. I didn’t find anything about this in swift evolution, so I thought i bring it up here. I think it would quite easily be possible to introduce a synchronized qualifier for struct/class objects that automatically synthesize a semaphore variable on the object and use it to lock said method. Here is an example of how that would work

Without the synchronized attribute (code written in mail, not compiled):

class Obj {

	 private let sema = DispatchSemaphore(value: 1)

	func synchronizedMethod() {
		defer {
		// do something...

With synchronized attribute (the semaphore/wait/deferred-signal is synthesized by Swift automatically)

class Obj {

	synchronized func method() {
		// semaphore is synthesized automatically, do something…


More information about the swift-evolution mailing list