Concurrent icon indicating copy to clipboard operation
Concurrent copied to clipboard

Swap is not atomic, is this by design?

Open bkase opened this issue 8 years ago • 2 comments

/// 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.

bkase avatar Jun 11 '17 03:06 bkase

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.

CodaFi avatar Sep 22 '17 18:09 CodaFi

Resolved by the merge of #52

CodaFi avatar Sep 28 '17 02:09 CodaFi