<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">With a serial queue guarding the function or concurrent queues dumping into a serial target queue,&nbsp;<font face="Menlo" class="">DispatchQueue.sync(execute:)</font> can be used as a synchronizing primitive suitable for most workflows. &nbsp;Have you tried exploring libDispatch outside of Semaphores?<div class=""><br class=""></div><div class="">~Robert Widmann<br class=""><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On Jun 12, 2017, at 2:10 AM, Erik Aigner via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="">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<br class=""><br class="">Without the synchronized attribute (code written in mail, not compiled):<br class=""><br class="">class Obj {<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span> private let sema = DispatchSemaphore(value: 1)<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>func synchronizedMethod() {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>sema.wait()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>defer {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>sema.signal()<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>// do something...<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class="">With synchronized attribute (the semaphore/wait/deferred-signal is synthesized by Swift automatically)<br class=""><br class="">class Obj {<br class=""><br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>synchronized func method() {<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span><span class="Apple-tab-span" style="white-space:pre">        </span>// semaphore is synthesized automatically, do something…<br class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}<br class="">}<br class=""><br class=""><br class="">Cheers,<br class="">Erik<br class="">_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></div></blockquote></div><br class=""></div></div></body></html>