<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto">It's up to the programmer-user to impose thread-safety around an array or other data structure. The compiler doesn't know if you want to allow multiple threads to write and read multiple array members, or prevent all but one thread to read and write multiple array members, or allow multiple readers when there are no writers, etc. (And we have queues precisely for these scenarios.)<div><br></div><div>The mutation counter detects if modifications happen while iterating over a collection, modifications like adding or removing members. When this happens, it's probably a programmer error. It's a bonus if this can be detected in a multiple thread scenario.<br><div><br><div id="AppleMailSignature"><span style="background-color: rgba(255, 255, 255, 0);">--<br>C. Keith Ray</span><div><span style="background-color: rgba(255, 255, 255, 0);"><br></span></div><div><span style="background-color: rgba(255, 255, 255, 0);">* <a href="https://leanpub.com/wepntk">https://leanpub.com/wepntk</a> <- buy my book?<br>* </span><a href="http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf">http://www.thirdfoundationsw.com/keith_ray_resume_2014_long.pdf</a><span style="background-color: rgba(255, 255, 255, 0);"><br>* <a href="http://agilesolutionspace.blogspot.com/">http://agilesolutionspace.blogspot.com/</a></span></div></div><div><br>On Jan 8, 2018, at 6:25 PM, Fadi Botros via swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:<br><br></div><blockquote type="cite"><div><div style="color:#000; background-color:#fff; font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px">What about double checking?<div class="qtdSeparateBR"><br><br></div><div class="yahoo_quoted" id="yui_3_16_0_ym19_1_1515461367163_13194" style="display: block;"><div style="font-family: Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 13px;" id="yui_3_16_0_ym19_1_1515461367163_13193"><div style="font-family: HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif; font-size: 16px;" id="yui_3_16_0_ym19_1_1515461367163_13192"><div class="y_msg_container" id="yui_3_16_0_ym19_1_1515461367163_13206"><div id="yiv0211351497"><div id="yui_3_16_0_ym19_1_1515461367163_13205"><div style="color:#000;background-color:#fff;font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px;" id="yui_3_16_0_ym19_1_1515461367163_13204"><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10288"><br clear="none"></div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10289">Something like that</div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10393"><br clear="none"></div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10394">if counter != 0 { throw exc }<br clear="none"></div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10395">counter += 1</div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10396">if counter != 1 { throw exc }</div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10397">counter += 1<br clear="none"></div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10260"><span><br clear="none"></span></div><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10399"><span id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10398">I think the performance penalty is just a check and an increment<br clear="none"></span></div><div><span id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10398">It could be disabled after optimization or in release version to increase performance</span></div><div class="yiv0211351497qtdSeparateBR" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10233"><br clear="none"><br clear="none"></div><div class="yiv0211351497yqt6302841969" id="yiv0211351497yqt45576"><div class="yiv0211351497yahoo_quoted" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10225" style="display:block;"> <div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10224" style="font-family:Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:13px;"> <div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10223" style="font-family:HelveticaNeue-Light, Helvetica Neue Light, Helvetica Neue, Helvetica, Arial, Lucida Grande, sans-serif;font-size:16px;"> <div dir="rtl" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10222"> <font id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10221" size="2" face="Arial"> </font><hr id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10400" size="1"> <b id="yui_3_16_0_ym19_1_1515461367163_13208"><span style="font-weight:bold;" id="yui_3_16_0_ym19_1_1515461367163_13207">من:</span></b> Greg Parker <<a href="mailto:gparker@apple.com">gparker@apple.com</a>><br clear="none"> <b id="yui_3_16_0_ym19_1_1515461367163_13210"><span style="font-weight:bold;" id="yui_3_16_0_ym19_1_1515461367163_13209">إلى:</span></b> Chris Lattner <<a href="mailto:clattner@nondot.org">clattner@nondot.org</a>> <br clear="none"><b id="yui_3_16_0_ym19_1_1515461367163_13212"><span style="font-weight:bold;" id="yui_3_16_0_ym19_1_1515461367163_13211">نسخة كربونية:</span></b> Jordan Rose <<a href="mailto:jordan_rose@apple.com">jordan_rose@apple.com</a>>; swift-users <<a href="mailto:swift-users@swift.org">swift-users@swift.org</a>>; Fadi Botros <<a href="mailto:botros_fadi@yahoo.com">botros_fadi@yahoo.com</a>><br clear="none"> <b id="yui_3_16_0_ym19_1_1515461367163_13214"><span style="font-weight:bold;" id="yui_3_16_0_ym19_1_1515461367163_13213">تاريخ الإرسال:</span></b> الثلاثاء 9 يناير، 2018 3:47 ص<br clear="none"> <b id="yui_3_16_0_ym19_1_1515461367163_13216"><span style="font-weight:bold;" id="yui_3_16_0_ym19_1_1515461367163_13215">الموضوع:</span></b> Re: [swift-users] بخصوص: Swift 4.0 bug in concurrent array access<br clear="none"> </div> <div class="yiv0211351497y_msg_container" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10226"><br clear="none"><div id="yiv0211351497"><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10227"><br class="yiv0211351497" clear="none"><div id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10406"><blockquote class="yiv0211351497" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10405" type="cite"><div class="yiv0211351497" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10404">On Jan 8, 2018, at 5:20 PM, Chris Lattner <<a rel="nofollow" shape="rect" class="yiv0211351497" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10403" ymailto="mailto:clattner@nondot.org" target="_blank" href="mailto:clattner@nondot.org">clattner@nondot.org</a>> wrote:</div><div class="yiv0211351497" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10408"><div class="yiv0211351497" id="yiv0211351497yui_3_16_0_ym19_1_1515461367163_10407" style="word-wrap:break-word;"><br class="yiv0211351497" clear="none"><div class="yiv0211351497"><blockquote class="yiv0211351497" type="cite"><div class="yiv0211351497">On Jan 8, 2018, at 2:59 PM, Greg Parker via swift-users <<a rel="nofollow" shape="rect" class="yiv0211351497" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:</div><div class="yiv0211351497"><div class="yiv0211351497" style="word-wrap:break-word;"><br class="yiv0211351497" clear="none"><div class="yiv0211351497"><blockquote class="yiv0211351497" type="cite"><div class="yiv0211351497">On Jan 8, 2018, at 12:51 PM, Jordan Rose via swift-users <<a rel="nofollow" shape="rect" class="yiv0211351497" ymailto="mailto:swift-users@swift.org" target="_blank" href="mailto:swift-users@swift.org">swift-users@swift.org</a>> wrote:</div><div class="yiv0211351497"><div class="yiv0211351497" style="word-wrap:break-word;"><div class="yiv0211351497"><br class="yiv0211351497" clear="none"><blockquote class="yiv0211351497" type="cite"><div class="yiv0211351497">On Jan 8, 2018, at 11:47, Fadi Botros <<a rel="nofollow" shape="rect" class="yiv0211351497" ymailto="mailto:botros_fadi@yahoo.com" target="_blank" href="mailto:botros_fadi@yahoo.com">botros_fadi@yahoo.com</a>> wrote:</div><br class="yiv0211351497Apple-interchange-newline" clear="none"><div class="yiv0211351497"><div class="yiv0211351497"><div class="yiv0211351497" style="background-color:rgb(255, 255, 255);"><div class="yiv0211351497">1st: How to invoke the thread sanitizer?</div></div></div></div></blockquote><div class="yiv0211351497"><br class="yiv0211351497" clear="none"></div><div class="yiv0211351497">Check out this article on <a rel="nofollow" shape="rect" class="yiv0211351497" target="_blank" href="http://developer.apple.com/">developer.apple.com</a>: <a rel="nofollow" shape="rect" class="yiv0211351497" target="_blank" href="https://developer.apple.com/documentation/code_diagnostics/thread_sanitizer/enabling_the_thread_sanitizer">https://developer.apple.com/documentation/code_diagnostics/thread_sanitizer/enabling_the_thread_sanitizer</a></div><br class="yiv0211351497" clear="none"><blockquote class="yiv0211351497" type="cite"><div class="yiv0211351497"><div class="yiv0211351497"><div class="yiv0211351497" style="background-color:rgb(255, 255, 255);"><div class="yiv0211351497" dir="ltr" id="yiv0211351497yui_3_16_0_ym19_1_1515440347138_7774">2nd: I think making something like ConcurrentModificationException of Java should be a little better experience (if it will not make, or make a small performance penalty, also if it makes performance penalty, it would be activated only when you make a debug version or non-optimized version)<br class="yiv0211351497" clear="none"></div></div></div></div></blockquote><div class="yiv0211351497"><br class="yiv0211351497" clear="none"></div><div class="yiv0211351497">I don't exactly disagree, but my understanding is that the work to do such a thing is nearly equivalent to having thread sanitizer on all the time. But maybe there's a simpler model that we could still turn on in debug builds. Can you file a bug report requesting this now that <a rel="nofollow" shape="rect" class="yiv0211351497" target="_blank" href="http://bugs.swift.org/">bugs.swift.org</a> is back up?</div></div></div></div></blockquote><br class="yiv0211351497" clear="none"></div><div class="yiv0211351497">Something like ConcurrentModificationException ought to be cheaper than the thread sanitizer. The thread sanitizer works hard to detect every concurrency error. ConcurrentModificationException typically does something simple and cheap that catches errors sometimes but makes no attempt to be exhaustive.</div><div class="yiv0211351497"><br class="yiv0211351497" clear="none"></div><div class="yiv0211351497">Objective-C's fast enumeration protocol includes a mechanism like this. The collection can have a simple mutation counter, and the enumerator captures the counter's value at the start and checks that the value is unchanged as the enumeration proceeds.</div></div></div></blockquote><br class="yiv0211351497" clear="none"></div><div class="yiv0211351497">Does ObjC synchronize the mutation counter? My understanding is that that counter was used to detect mutation while iteration on a single thread, not across threads.</div></div></div></blockquote><br class="yiv0211351497" clear="none"></div><div>The mutation counter is not synchronized; this is safe for correct code. An un-synchronized counter can still sometimes catch mutations from other threads.</div><div class="yiv0211351497yqt1234784834" id="yiv0211351497yqtfd65293"><div><br class="yiv0211351497" clear="none"></div><div><br class="yiv0211351497" clear="none"></div><div>-- </div></div><div><div class="yiv0211351497yqt1234784834" id="yiv0211351497yqtfd05657">Greg Parker </div><a rel="nofollow" shape="rect" class="yiv0211351497" ymailto="mailto:gparker@apple.com" target="_blank" href="mailto:gparker@apple.com">gparker@apple.com</a> Runtime Wrangler</div><div class="yiv0211351497yqt1234784834" id="yiv0211351497yqtfd21976"><div><br class="yiv0211351497" clear="none"></div><br class="yiv0211351497" clear="none"></div></div></div><br clear="none"><br clear="none"></div> </div> </div> </div></div></div></div></div><br><br></div> </div> </div> </div></div></div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-users mailing list</span><br><span><a href="mailto:swift-users@swift.org">swift-users@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-users">https://lists.swift.org/mailman/listinfo/swift-users</a></span><br></div></blockquote></div></div></body></html>