A simple mutation counter works in the single-threaded case for i in x { print(i) if middle_of_x { x.add(y) } // bang! }
The add while enumerating should change the mutation counter, and the implicit getting of next value for the for-loop should see the current mutation counter doesn't equal the initial cached value of the mutation counter. In the multi-threaded case, the mutation counter needs to be thread-safe, otherwise comparing its current value to a cached value could fail from timing issues. // thread one for i in x { print(i) } // thread two x.add(y) // bang? > On Jan 8, 2018, at 2:59 PM, Greg Parker via swift-users > <swift-users@swift.org> wrote: > >> >> On Jan 8, 2018, at 12:51 PM, Jordan Rose via swift-users >> <swift-users@swift.org <mailto:swift-users@swift.org>> wrote: >> >>> On Jan 8, 2018, at 11:47, Fadi Botros <botros_f...@yahoo.com >>> <mailto:botros_f...@yahoo.com>> wrote: >>> >>> 1st: How to invoke the thread sanitizer? >> >> Check out this article on developer.apple.com <http://developer.apple.com/>: >> 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> >>> 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) >> >> 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 bugs.swift.org >> <http://bugs.swift.org/> is back up? > > 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. > > 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. > > > -- > Greg Parker gpar...@apple.com <mailto:gpar...@apple.com> Runtime > Wrangler > > > _______________________________________________ > swift-users mailing list > swift-users@swift.org <mailto:swift-users@swift.org> > https://lists.swift.org/mailman/listinfo/swift-users > <https://lists.swift.org/mailman/listinfo/swift-users>
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users