Open
Description
Compare-and-set is a useful functionality (that's why it exists in the Java API). It can save one from race conditions, as explained below.
Use case
- Assume an atomicboolean called
LOCKED
, set to false. - Multiple concurrent threads run the following code:
if !LOCKED.value
LOCKED.value = true
# do something ....
LOCKED.value = false
end
In this scenario, concurrent threads can read a false in if !LOCKED.value
, and enter the if
block. That defeats the purpose of an atomicboolean-backed lock.
If there was a CAS API, the fixed code would look like this:
if LOCKED.compare_and_set(false, true)
Furthermore
Ruby has no "tryLock" either. So it's not immediately obvious how to create non-blocking synchronized code. Boolean CAS would offer a nice alternative which Java programmers (and others) already are familiar with.
What do you think?
Cheers - Victor