2 ms·
Try compiling this with swiftc -sanitize=thread: import Foundation let q1 = DispatchQueue(label: "1") let q2 = DispatchQueue(label: "2") var a =
by saagarjha 8y ago
Try compiling this with swiftc -sanitize=thread:
import Foundation
let q1 = DispatchQueue(label: "1")
let q2 = DispatchQueue(label: "2")
var a = [0]
q1.async {
a[0] = 1
}
q2.async {
a[0] = 2
}
You'll get an error at runtime that there's a data race.
- raphlinus 8y agoThis is a subtle point. It's not the array assignment that's the race here, but the update to the shared (by closure capture) `a` variable. You'd get the same data race if `a` were an `Int`, and we don't say that integers are non-thread-safe. If each closure had its own reference to the array, it would be copied on write. I believe this is pretty good evidence for my claim that understanding the thread safety is confusing.