43 static_assert(std::is_copy_constructible_v<T> || std::is_move_constructible_v<T>,
44 "T must be copy or move constructible");
49 rcu_value()
requires std::is_default_constructible_v<T>
50 :
current_(std::make_shared<const T>()) {}
57 :
current_(std::make_shared<const T>(std::move(initial))) {}
64 :
current_(std::atomic_load_explicit(&other.
current_, std::memory_order_acquire)) {}
71 :
current_(std::atomic_load_explicit(&other.current_, std::memory_order_acquire)) {}
80 auto new_ptr = std::atomic_load_explicit(&other.
current_, std::memory_order_acquire);
81 std::atomic_store_explicit(&
current_, new_ptr, std::memory_order_release);
93 auto new_ptr = std::atomic_load_explicit(&other.current_, std::memory_order_acquire);
94 std::atomic_store_explicit(&
current_, new_ptr, std::memory_order_release);
115 [[nodiscard]] std::shared_ptr<const T>
read() const noexcept {
116 return std::atomic_load_explicit(&
current_, std::memory_order_acquire);
130 auto new_ptr = std::make_shared<const T>(std::move(new_value));
131 std::atomic_store_explicit(&
current_, new_ptr, std::memory_order_release);
149 auto new_ptr = std::make_shared<const T>(std::move(new_value));
150 auto expected_copy = expected;
151 bool success = std::atomic_compare_exchange_strong_explicit(
155 std::memory_order_acq_rel,
156 std::memory_order_acquire
177 return std::atomic_load_explicit(&
current_, std::memory_order_acquire) !=
nullptr;
Lock-free value wrapper using Read-Copy-Update (RCU) pattern.
bool has_value() const noexcept
Check if the value has been initialized.
rcu_value()
Default constructor - initializes with default-constructed T.
size_t update_count() const noexcept
Get the number of updates performed.
~rcu_value()=default
Destructor.
std::shared_ptr< const T > current_
rcu_value(T initial)
Construct with initial value.
std::shared_ptr< const T > read() const noexcept
Lock-free read - returns current snapshot.
void update(T new_value)
Update the value atomically.
bool compare_and_update(const std::shared_ptr< const T > &expected, T new_value)
Compare-and-swap update.
rcu_value & operator=(rcu_value &&other) noexcept
Move assignment operator.
rcu_value & operator=(const rcu_value &other)
Copy assignment operator.
rcu_value(const rcu_value &other)
Copy constructor.
rcu_value(rcu_value &&other) noexcept
Move constructor.
std::atomic< size_t > update_count_