GMP 0.3.0
Generative Metaprogramming library for C++
Loading...
Searching...
No Matches
lock.hpp
Go to the documentation of this file.
1// ___ __ __ ___
2// / __| \/ | _ \ GMP(Generative Metaprogramming)
3// | (_ | |\/| | _/ version 0.3.0
4// \___|_| |_|_| https://github.com/lkimuk/gmp
5//
6// SPDX-FileCopyrightText: 2020-2026 Miles Li <https://www.cppmore.com/>
7// SPDX-License-Identifier: MIT
8//
9// This file is part of the GMP (Generative Metaprogramming) library.
10// Full project source: https://github.com/lkimuk/gmp
11//
12// This spin lock implementation was originally written by the same author
13// for the okdp library in 2020 and later adapted for GMP:
14// https://github.com/lkimuk/okdp
15
16#ifndef GMP_DP_LOCK_HPP_
17#define GMP_DP_LOCK_HPP_
18
19#include <atomic>
20
21namespace gmp::dp {
22
39class spin_lock {
40private:
41 std::atomic_flag flag_ = ATOMIC_FLAG_INIT;
42
43public:
47 void lock() {
48 while (flag_.test_and_set(std::memory_order_acquire));
49 }
50
54 void unlock() {
55 flag_.clear(std::memory_order_release);
56 }
57};
58
61} // namespace gmp::dp
62
63#endif // GMP_DP_LOCK_HPP_
A minimal spin lock built on std::atomic_flag.
Definition lock.hpp:39
void unlock()
Release the lock.
Definition lock.hpp:54
void lock()
Acquire the lock by spinning until it becomes available.
Definition lock.hpp:47
consteval auto enum_values()
Get all enumerator values of an enumeration type at compile-time.
Definition meta.hpp:155