Real-world examples for ABA in multithreading [closed] Real-world examples for ABA in multithreading [closed] multithreading multithreading

Real-world examples for ABA in multithreading [closed]


Suppose you want to implement an ordered list using a traditional linked list. Suppose you want to add a new value V to the list. First, you have to find the right position to insert the new element using an auxiliary pointer AUX and locate it in the last node with a value smaller than V, and also save AUX->next to make the comparison in the CAS operation. Once you have the reference you make NEW->next points to AUX->next and then using a CAS you switch AUX->next to NEW if AUX->next is still the same reference you saved. It should be something like this:

AUX = list.HEAD;WHILE( AUX->next.value < V)    AUX = AUX->next;OLD = AUX->next; //line 4NEW->next = AUX->next; //line 5IF( CAS(AUX->next, NEW, OLD)) //line 6    Success!!!ELSE    Try again or whatever

This is the simplest way to do it. The problem is that between lines 4 and 5, another thread might have removed "OLD" and then have inserted another element X smaller than V but still greater than AUX.value. If this happens, and the memory assigned to the node with value X is in the same address that used to have OLD, the CAS will succeed, but the list will not be ordered. If the actions of the second thread occur between lines 5 and 6, you will lose the node with the value X. All of these is because of the ABA problem.