rcu_ptr icon indicating copy to clipboard operation
rcu_ptr copied to clipboard

Measure (and handle) starvation issues with CAS loops

Open martong opened this issue 9 years ago • 2 comments

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.

martong avatar Feb 16 '16 16:02 martong

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

martong avatar Feb 17 '16 10:02 martong

How to detect starvation? Count the iterations of the CAS loop.

martong avatar Feb 17 '16 10:02 martong