The code has to be designed in a way that it can be repeated without any
side effects. When the CAS operation was successful it should notify
this mutex by calling {@link CASMutex::notify()}. I.e. the only side effects
of the code may happen after a successful CAS operation. The CAS
operation itself is a valid side effect as well.
If the code throws an exception it will stop repeating the execution.
Example:
$mutex = new CASMutex();
$mutex->synchronized(function () use ($memcached, $mutex, $amount) {
$balance = $memcached->get("balance", null, $casToken);
$balance -= $amount;
if (!$memcached->cas($casToken, "balance", $balance)) {
return;
}
$mutex->notify();
});