ABA problem
ABA problem
ABA Problem Solutions
The root solution to ABA problem is that we should defer reclamation while other threads are holding the node or we should have a way to identify the difference between the old node and the new node.
There are several solutions to the ABA problem, for the details, you can see this link. https://en.wikipedia.org/wiki/ABA_problem
Here, I would introduce a common solution for you.
A common workaround is to add an extra "tag" or "stamp" word to the quantity being considered. For example, an algorithm using compare and swap on a pointer might use a "tag" to indicate how many times the pointer has been successfully modified. Because of this, the next compare-and-swap will fail, even if the addresses are the same, because the tag word will not match. This is sometimes called ABAʹ since the second A is made slightly different from the first.
Here, we solve the ABA problem by using the above solution. We reconstruct the stack by adding a "tag":
Every time when we use the pointer, we will increase the "tag" by 1.
Don't forget to change the initialization of the stack.
You may meet the error "undefined reference to __atomic_compare_exchange_16
" when you compile the code. If so, compile your code with -latomic
Now, when we run the program again, it will not print any error information. Here is a C library called liblfds, https://www.liblfds.org/, a lock-free data structure library written in C. If you don't like to write the lock-free data structure by yourself, you could use this C library.
Last updated