mikeash.com pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html commentshttp://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsmikeash.com Recent CommentsFri, 29 Mar 2024 05:38:28 GMTPyRSS2Gen-1.0.0http://blogs.law.harvard.edu/tech/rssQueen K Wig - 2021-12-16 11:24:37http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsI was searching for the helpful article for thread safety. I am happy after reading your post. Are you looking for the best salon if yes visit at queenkwig.com <br /> <br />6e6929e2e1d7476a58448da1065cc95cThu, 16 Dec 2021 11:24:37 GMTShawn Rong - 2021-06-29 09:39:19http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsGreat, I appreciate the work that you put into writing this wonderful article for safety purposes. Also, check my blog for women pregnancy-related...mamadailydose.com/geriatric-pregnancy/931eb75f39bbb82ce6378929b954ce41Tue, 29 Jun 2021 09:39:19 GMTJohnO - 2018-03-02 20:33:27http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsAn extension on NSLocking protocol might even be more general. :)9ab6df9e86cfdeeb910953f6a6a6b523Fri, 02 Mar 2018 20:33:27 GMTJohnO - 2018-03-02 19:57:28http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsNice write up and enjoy the blog. <br /> <br />Perhaps an extension might make usability easier. Love Swift defer. <br /> <br />extension NSLock { <br />&nbsp;&nbsp;&nbsp;&nbsp;func lock&lt;T&gt;(_ f: () throws -&gt; T) rethrows -&gt; T { <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.lock() <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;defer { self.unlock() } <br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return try f() <br />&nbsp;&nbsp;&nbsp;&nbsp;} <br />} <br /> <br />---- <br /> <br />let result = myLock.lock() { <br />&nbsp;&nbsp;//stuff to do in trailing cloture <br />&nbsp;&nbsp;return true <br />} <br />5ad697a9f44dc81f11e7bba33d467585Fri, 02 Mar 2018 19:57:28 GMTmikeash - 2017-11-02 00:29:42http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#comments<b>Jean-Daniel</b>: That's a really interesting post. It sounds like they may have messed up in offering custom concurrent queues as part of the API, but I'm not sure that this translates into avoiding them as an API user. They're there and they do work (as far as I know!) so it's more a problem for the GCD folks to keep it working. The "it" that they're not going to attempt right away is an actor API analogous to GCD concurrent queues. <br /> <br /><b>Russell Finn</b>: Sticking it on one line like that is pretty nice. I still don't like it just because it's conceptually two things, and more importantly the compiler will let you write one without the other. I certainly won't try to argue that it's <i>bad</i>, I just like mechanisms where it's impossible to use it wrong. <br /> <br />I agree with your proposed <code>with(locked:)</code> renaming. That seems more natural. Or just call it <code>with</code>. Or make an operator! <code>let value = lock**{ return self.property }</code> <br /> <br />Oh, have I gone too far again? Oops.47f2f9fbaa5269e120c355022284f213Thu, 02 Nov 2017 00:29:42 GMTRussell Finn - 2017-10-31 21:58:29http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#comments"It's still annoying to have to write two lines"... which is why I'd write this on a single line: <br /><div class="blogcommentquote"><div class="blogcommentquoteinner"><code> lock.lock(); defer { lock.unlock() }</code></div></div> <br />The unusual use of the semicolon separator makes the idiom stand out. Erica Sadun agrees with this usage in her book <i>Swift Style</i>, although I came up with it independently. <br /> <br />The <code>withLocked&lt;T&gt;</code> function is a reasonably clever alternative, although I think I'd spell it <code>with&lt;T&gt;(locked lock: NSLock ...)</code> these days: <br /> <br /><div class="blogcommentquote"><div class="blogcommentquoteinner"><code> let value = with(locked: lock) { return self.property }</code></div></div> <br />Great update; thanks.113720b7266e0f35b0dff6b15749b0cdTue, 31 Oct 2017 21:58:29 GMTJean-Daniel - 2017-10-29 07:39:36http://www.mikeash.com/?page=pyblog/friday-qa-2017-10-27-locks-thread-safety-and-swift-2017-edition.html#commentsWhile I though the same as you about custom concurrent queues, until I read this statement from a libdispatch authors: <br /> <br /><div class="blogcommentquote"><div class="blogcommentquoteinner">Private concurrent queues are not a success in dispatch and cause several issues, these queues are second class citizens in GCD in terms of feature they support, and building something with concurrency *within* is hard. I would keep it as "that's where we'll go some day" but not try to attempt it until we've build the simpler (or rather less hard) purely serial case first.</div></div> <br /> <br />So, I'm not sure I would recommend using it at this point. <br /> <br />This quote is part of the Swift discussion about async/await ( <a href="https://pastebin.com/MMid7Z6p">https://pastebin.com/MMid7Z6p</a> ). <br /> <br />One must read for all dispatch users is the last WWDC session about libdispatch which give important insight about how the system expects you to use it. <br />1157e78e64c5c17f0dd2ea0eb031f654Sun, 29 Oct 2017 07:39:36 GMT