Skip to content

Instantly share code, notes, and snippets.

@macu
Created August 21, 2014 16:58
Show Gist options
  • Select an option

  • Save macu/3843eec563e74a5e5804 to your computer and use it in GitHub Desktop.

Select an option

Save macu/3843eec563e74a5e5804 to your computer and use it in GitHub Desktop.
class AtomicBoolean {
private var val: Byte = 0
/// Sets the value, and returns the previous value.
/// The test/set is an atomic operation.
func testAndSet(value: Bool) -> Bool {
if value {
return OSAtomicTestAndSet(0, &val)
} else {
return OSAtomicTestAndClear(0, &val)
}
}
/// Returns the current value of the boolean.
/// The value may change before this method returns.
func test() -> Bool {
return val != 0
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment