diff --git a/include/spinLock.h b/include/spinLock.h new file mode 100644 index 0000000..afda9a5 --- /dev/null +++ b/include/spinLock.h @@ -0,0 +1,35 @@ +#ifndef SPIN_LOCK_H +#define SPIN_LOCK_H + +#include + +namespace smo { + +/** + * @brief Simple spinlock using std::atomic + */ +class SpinLock +{ +public: + SpinLock() + : locked(false) + {} + + bool tryAcquire() + { + bool expected = false; + return locked.compare_exchange_strong(expected, true); + } + + void release() + { + locked.store(false); + } + +private: + std::atomic locked; +}; + +} // namespace smo + +#endif // SPIN_LOCK_H