Many thanks to Chris M. Thomasson for rewriting POSIX Threads for Win32 mutex into standard C++ implementation. Using auto_event class from Event objects.
mutex.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#pragma once #include <atomic> #include "event.h" class fast_mutex { public: fast_mutex() : m_state(0) {} void lock() { if (m_state.exchange(1, std::memory_order_acquire)) while (m_state.exchange(2, std::memory_order_acquire)) m_waitset.wait(); } void unlock() { if (m_state.exchange(0, std::memory_order_release) == 2) m_waitset.signal(); } private: std::atomic<unsigned int> m_state; auto_event m_waitset; }; |