In a well-known book The Art of Multiprocessor Programming
by Herlihy, Shavit some of lock-free and wait-free algorithms utilize Java's template AtomicMarkableReference<T>
type. It allows to perform single atomic CAS operation on the pair consisting of T
reference and boolean mark.
There is no similar type in C/C++/Go stdlib, but at least in C++ it's possible to model it using bit stealing approach (see C++ example). On x86_64
arch only 48 bits of 64 bits are actually used, so one can store arbitrary data in the remaining 16 bits, and work with the whole pointer and the data atomically.
As far as I understand, there are two requirements to implement this approach:
- Pointers must be aligned.
- Pointer's low bits must be clear (if you want to store something like
bool
in this area, it must not be already occupied).
Are these requirements met in Go? Are there any working examples of bit stealing technique in Go?