libpromeki main
PROfessional MEdia toolKIt
 
Loading...
Searching...
No Matches
atomic.h
Go to the documentation of this file.
1
8#pragma once
9
10#include <atomic>
12
14
25template <typename T>
26class Atomic {
27 public:
32 Atomic(T val = T{}) : _value(val) {}
33
35 ~Atomic() = default;
36
37 Atomic(const Atomic &) = delete;
38 Atomic &operator=(const Atomic &) = delete;
39 Atomic(Atomic &&) = delete;
40 Atomic &operator=(Atomic &&) = delete;
41
46 T value() const {
47 return _value.load(std::memory_order_acquire);
48 }
49
54 void setValue(T val) {
55 _value.store(val, std::memory_order_release);
56 }
57
64 T fetchAndAdd(T val) {
65 return _value.fetch_add(val, std::memory_order_acq_rel);
66 }
67
74 T fetchAndSub(T val) {
75 return _value.fetch_sub(val, std::memory_order_acq_rel);
76 }
77
89 bool compareAndSwap(T &expected, T desired) {
90 return _value.compare_exchange_strong(expected, desired,
91 std::memory_order_acq_rel, std::memory_order_acquire);
92 }
93
99 T exchange(T desired) {
100 return _value.exchange(desired, std::memory_order_acq_rel);
101 }
102
103 private:
104 std::atomic<T> _value;
105};
106
Atomic variable wrapping std::atomic<T>.
Definition atomic.h:26
bool compareAndSwap(T &expected, T desired)
Atomically compares and swaps.
Definition atomic.h:89
Atomic(T val=T{})
Constructs an Atomic with the given initial value.
Definition atomic.h:32
~Atomic()=default
Destructor.
T fetchAndAdd(T val)
Atomically adds val and returns the previous value.
Definition atomic.h:64
T exchange(T desired)
Atomically replaces the value and returns the previous one.
Definition atomic.h:99
void setValue(T val)
Stores a new value with release semantics.
Definition atomic.h:54
T fetchAndSub(T val)
Atomically subtracts val and returns the previous value.
Definition atomic.h:74
T value() const
Loads the current value with acquire semantics.
Definition atomic.h:46
#define PROMEKI_NAMESPACE_BEGIN
Starts a promeki namespace block.
Definition namespace.h:14
#define PROMEKI_NAMESPACE_END
Ends a promeki namespace block.
Definition namespace.h:19