Spinlock Using atomic flag

From Software Engineers Wiki
Jump to: navigation, search

Implement spinlocks using atomic_flag.

Answer

#include <atomic>

class spinlock { 
private:
        std::atomic_flag lock_flag;
public:
        spinlock() : lock_flag { ATOMIC_FLAG_INIT } {}
        void lock(void) { while (lock_flag.test_and_set()) ; }
        void unlock(void) { lock_flag.clear(); }
};
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox