Measure (and handle) starvation issues with CAS loops
From http://preshing.com/20150402/you-can-do-any-kind-of-atomic-read-modify-write-operation/
Suppose thread A has to do a bunch of extra work in the "modify" step for some reason, such that one of threads B, C, ... has almost always gotten in and updated the variable by the time A gets to the CAS call. Then A might have to retry many, many times before it succeeds.
... Preshing:
I'm pretty sure individual threads could be made to starve indefinitely.
About handling: Once the starvation is detected then a lock (lightweight mutex) could be locked by the starving thread to ensure exclusive access to the shared variable. This might not work, because
- that lock would enforce stricter ordering rules than we had without it
- we would have starvation on the lock
How to detect starvation? Count the iterations of the CAS loop.