This improved version of the adaptive spin mutex is inspired by an article by Foster Brereton.
It has been field-tested for many years in real-world applications and has been slightly improved to achieve better performance.
Since the class is implemented as a header module (C++11 is required), it is enough to throw it into your project and include it with compiler directive.
Since the class is implemented with an interface that is fully compatible with mutex wrapper classes, it is easily used as a replacement for standard mutexes:
AdaptiveSpinMutex spmux(m_lock);
std::lock_guard<AdaptiveSpinMutex> lock(spmux);
...
}
private:
std::atomic<bool>& m_lock;
...
Class optimized for any C++11 compliant compiler, tested on GCC, Clang, Oracle Developer Studio.