|
Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
|
Lock-free value wrapper using Read-Copy-Update (RCU) pattern. More...
#include <rcu_value.h>

Public Member Functions | |
| rcu_value () | |
| Default constructor - initializes with default-constructed T. | |
| rcu_value (T initial) | |
| Construct with initial value. | |
| rcu_value (const rcu_value &other) | |
| Copy constructor. | |
| rcu_value (rcu_value &&other) noexcept | |
| Move constructor. | |
| rcu_value & | operator= (const rcu_value &other) |
| Copy assignment operator. | |
| rcu_value & | operator= (rcu_value &&other) noexcept |
| Move assignment operator. | |
| ~rcu_value ()=default | |
| Destructor. | |
| 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. | |
| size_t | update_count () const noexcept |
| Get the number of updates performed. | |
| bool | has_value () const noexcept |
| Check if the value has been initialized. | |
Private Attributes | |
| std::shared_ptr< const T > | current_ |
| std::atomic< size_t > | update_count_ {0} |
Lock-free value wrapper using Read-Copy-Update (RCU) pattern.
This template provides truly lock-free reads using atomic shared_ptr operations. Writers create new versions, and readers access the current version atomically. Old versions are automatically reclaimed when the last reader releases them.
Properties:
| T | The value type to store (must be copy-constructible or move-constructible) |
Example usage:
Definition at line 40 of file rcu_value.h.
|
inline |
Default constructor - initializes with default-constructed T.
Definition at line 49 of file rcu_value.h.
|
inlineexplicit |
Construct with initial value.
| initial | The initial value |
Definition at line 56 of file rcu_value.h.
|
inline |
Copy constructor.
| other | The rcu_value to copy from |
Definition at line 63 of file rcu_value.h.
|
inlinenoexcept |
Move constructor.
| other | The rcu_value to move from |
Definition at line 70 of file rcu_value.h.
|
default |
Destructor.
|
inline |
Compare-and-swap update.
Atomically updates the value only if the current value equals expected. This is useful for implementing lock-free algorithms that need to detect concurrent modifications.
| expected | Shared pointer to expected current value |
| new_value | The new value to store if current matches expected |
Thread safety: Safe to call concurrently with any other operation
Definition at line 148 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_, and kcenon::container::rcu_value< T >::update_count_.
|
inlinenodiscardnoexcept |
Check if the value has been initialized.
Definition at line 176 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_.
|
inline |
Copy assignment operator.
| other | The rcu_value to copy from |
Definition at line 78 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_.
|
inlinenoexcept |
Move assignment operator.
| other | The rcu_value to move from |
Definition at line 91 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_.
|
inlinenodiscardnoexcept |
Lock-free read - returns current snapshot.
This operation is wait-free: it completes in O(1) time regardless of what other threads are doing. The returned shared_ptr keeps the snapshot alive even if the value is updated by another thread.
Thread safety: Safe to call concurrently with any other operation
Definition at line 115 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_.
|
inline |
Update the value atomically.
Creates a new immutable version and publishes it atomically. The old version is automatically reclaimed when the last reader releases it.
| new_value | The new value to store |
Thread safety: Safe to call concurrently with any other operation
Definition at line 129 of file rcu_value.h.
References kcenon::container::rcu_value< T >::current_, and kcenon::container::rcu_value< T >::update_count_.
|
inlinenodiscardnoexcept |
Get the number of updates performed.
Definition at line 168 of file rcu_value.h.
References kcenon::container::rcu_value< T >::update_count_.
|
private |
Definition at line 181 of file rcu_value.h.
Referenced by kcenon::container::rcu_value< T >::compare_and_update(), kcenon::container::rcu_value< T >::has_value(), kcenon::container::rcu_value< T >::operator=(), kcenon::container::rcu_value< T >::operator=(), kcenon::container::rcu_value< T >::read(), and kcenon::container::rcu_value< T >::update().
|
private |
Definition at line 182 of file rcu_value.h.
Referenced by kcenon::container::rcu_value< T >::compare_and_update(), kcenon::container::rcu_value< T >::update(), and kcenon::container::rcu_value< T >::update_count().