A std::mutex
-compatible mutex class implemented using atomic spin-waiting.
Based on Erik Rigtorp's article Correctly implementing a spinlock in C++ and incorporating some advice from the Intel 64 and IA-32 Architectures Optimization Reference Manual.
Requires C++11.
namespace mz
{
class spin_mutex
{
// acquires a lock on the mutex
void lock() noexcept;
// tries to acquire a lock on the mutex,
// returning true if the lock was successfully acquired
bool try_lock() noexcept;
// releases the lock currently held on the mutex
void unlock() noexcept;
};
}
The library is a single header so the easiest way to use it is to drop spin_mutex.hpp somewhere in your project.
Alternatively you can add include
to your include paths then #include <mz/spin_mutex.hpp>
There is also support for use as a meson.build
subproject.
MIT. See LICENSE.
There are three ways you can contribute:
- Reporting bug or making feature requests here
- Opening a pull request (
⚠️ caveat - see below) - Becoming a sponsor ❤️
spin_mutex.hpp
is programmatically extracted from a much larger project so I won't accept pull requests made for this
repository directly; if you wish to contribute a bugfix or a feature, please find the spin_mutex
implementation
in this project and propose your changes there instead.
I will then propagate them to this satellite library when they are merged.