As a theoretical matter, yes. If you could manage somehow to get the two threads running in lockstep like this
time thread A thread B
---- -------- --------
|| CAS
|| atomic_write
|| CAS
\/ atomic_write
Then CAS would never return true.
In practice this will never happen when threads share a CPU/Core, and is unlikely to happen when threads are running on different CPUs or Cores. In practice it's incredibly unlikely to happen for more than few cycles, and astronomically unlikely to happen for more than a scheduler quantum.
And that's if this code
void foo()
{
// Check if value is 'test' and swap in 0xdeadbeef
while(!CAS(&test, test, 0xdeadbeef)) {}
}
does what it appears to do, which is to fetch the current value of test
, and compare it to test
to see if it has changed. In the real world iterations of CAS would be separated by code that did actual work. The volatile
keyword would be needed to insure that the compiler fetched test before calling CAS rather than assuming that a copy it might still have in a register would still be valid.
Or the value that you would be testing against wouldn't be the current value of test, but rather some sort of last known value.
In other words, this code example is a test of the theory, but you wouldn't use CAS like this in practice, so even if you could get this to fail, it doesn't necessarily tell you how it could fail when used in real world algorithms.