Skip to content

Instantly share code, notes, and snippets.

@daniel-j-h
Created August 5, 2014 17:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniel-j-h/3fae545d1c2cd1684897 to your computer and use it in GitHub Desktop.
Save daniel-j-h/3fae545d1c2cd1684897 to your computer and use it in GitHub Desktop.
Spinlock: It spins and locks!
#include <atomic>
class spinlock_mutex {
public:
spinlock_mutex() : flag(ATOMIC_FLAG_INIT) {}
void lock() { while(flag.test_and_set(std::memory_order_acquire)); }
void unlock() { flag.clear(std::memory_order_release); }
private:
std::atomic_flag flag;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment