Concurrent
Concurrent copied to clipboard
Swap is not atomic, is this by design?
/// Atomically, take a value from the `MVar`, put a given new value in the
/// `MVar`, then return the `MVar`'s old value.
public func swap(_ x : A) -> A {
let old = self.take()
self.put(x)
return old
}
The thread using swap might context-switch right after the take completes before the put -- if another thread puts then, this put will block.
Aha, you're absolutely correct! The atomicity of this is dependent on exclusive putters. We could implement this atomically by making it a primitive (taking all those locks and junk), but for now I'm just going to use #52 to update the docs.
Keeping this issue open as a feature request for a truly atomic swap.
Resolved by the merge of #52