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

Public Types | |
| using | snapshot_type = std::unordered_map<std::string, value> |
| using | snapshot_ptr = std::shared_ptr<const snapshot_type> |
Public Member Functions | |
| lockfree_container_reader (std::shared_ptr< thread_safe_container > container) | |
| Construct reader from container. | |
| lockfree_container_reader (const lockfree_container_reader &other) | |
| Copy constructor. | |
| lockfree_container_reader (lockfree_container_reader &&other) noexcept | |
| Move constructor. | |
| lockfree_container_reader & | operator= (const lockfree_container_reader &other) |
| Copy assignment. | |
| lockfree_container_reader & | operator= (lockfree_container_reader &&other) noexcept |
| Move assignment. | |
| ~lockfree_container_reader ()=default | |
| Destructor. | |
| template<concepts::ValueVariantType T> | |
| std::optional< T > | get (std::string_view key) const noexcept |
| Lock-free typed value read. | |
| bool | contains (std::string_view key) const noexcept |
| Lock-free value existence check. | |
| size_t | size () const noexcept |
| Lock-free snapshot size check. | |
| bool | empty () const noexcept |
| Lock-free empty check. | |
| std::vector< std::string > | keys () const |
| Get all keys from snapshot (lock-free) | |
| template<typename Func > | |
| void | for_each (Func &&func) const |
| Iterate over snapshot (lock-free for snapshot access) | |
| void | refresh () |
| Refresh snapshot from source container. | |
| size_t | refresh_count () const noexcept |
| Get the number of refreshes performed. | |
| std::shared_ptr< thread_safe_container > | source () const noexcept |
| Get the source container. | |
Private Attributes | |
| std::shared_ptr< thread_safe_container > | container_ |
| snapshot_ptr | snapshot_ |
| std::atomic< size_t > | refresh_count_ {0} |
True lock-free reader using RCU (Read-Copy-Update) pattern.
Unlike snapshot_reader which still uses a shared_mutex for snapshot access, this class provides genuinely lock-free reads using atomic shared_ptr operations. The trade-off is that reads may return slightly stale data between refreshes.
Properties:
Use this when:
Definition at line 383 of file thread_safe_container.h.
| using kcenon::container::lockfree_container_reader::snapshot_ptr = std::shared_ptr<const snapshot_type> |
Definition at line 386 of file thread_safe_container.h.
| using kcenon::container::lockfree_container_reader::snapshot_type = std::unordered_map<std::string, value> |
Definition at line 385 of file thread_safe_container.h.
|
inlineexplicit |
Construct reader from container.
| container | The thread-safe container to read from |
Definition at line 392 of file thread_safe_container.h.
References refresh().

|
inline |
Copy constructor.
Definition at line 402 of file thread_safe_container.h.
|
inlinenoexcept |
Move constructor.
Definition at line 410 of file thread_safe_container.h.
|
default |
Destructor.
|
inlinenodiscardnoexcept |
Lock-free value existence check.
| key | The key to check |
Thread safety: Safe to call concurrently with any operation
Definition at line 479 of file thread_safe_container.h.
References snapshot_.
|
inlinenodiscardnoexcept |
Lock-free empty check.
Thread safety: Safe to call concurrently with any operation
Definition at line 506 of file thread_safe_container.h.
References size().

|
inline |
Iterate over snapshot (lock-free for snapshot access)
The callback is called for each key-value pair in the snapshot. The iteration itself is lock-free, but the callback may perform locking operations.
| Func | Callback function type: void(const std::string&, const value&) |
| func | Callback to apply to each key-value pair |
Thread safety: Safe to call concurrently with any operation
Definition at line 542 of file thread_safe_container.h.
References snapshot_.
|
inlinenodiscardnoexcept |
Lock-free typed value read.
This operation is wait-free: it completes in O(1) time regardless of what other threads are doing. The snapshot may be stale; call refresh() to update.
| T | The expected value type (must satisfy ValueVariantType) |
| key | The key to look up |
Thread safety: Safe to call concurrently with any operation including refresh()
Definition at line 458 of file thread_safe_container.h.
References snapshot_.
|
inlinenodiscard |
Get all keys from snapshot (lock-free)
Thread safety: Safe to call concurrently with any operation
Definition at line 517 of file thread_safe_container.h.
References snapshot_.
|
inline |
Copy assignment.
Definition at line 418 of file thread_safe_container.h.
References container_, and snapshot_.
|
inlinenoexcept |
Move assignment.
Definition at line 430 of file thread_safe_container.h.
References container_, and snapshot_.
|
inline |
Refresh snapshot from source container.
This operation acquires the container's read lock to create a new snapshot. After this call returns, subsequent lock-free reads will see the updated data.
Thread safety: Safe to call concurrently with reads, but multiple concurrent refreshes may waste work (only one will "win").
Definition at line 560 of file thread_safe_container.h.
References container_, refresh_count_, and snapshot_.
Referenced by lockfree_container_reader().

|
inlinenodiscardnoexcept |
Get the number of refreshes performed.
Definition at line 575 of file thread_safe_container.h.
References refresh_count_.
|
inlinenodiscardnoexcept |
Lock-free snapshot size check.
Thread safety: Safe to call concurrently with any operation
Definition at line 494 of file thread_safe_container.h.
References snapshot_.
Referenced by empty().

|
inlinenodiscardnoexcept |
Get the source container.
Definition at line 583 of file thread_safe_container.h.
References container_.
|
private |
Definition at line 588 of file thread_safe_container.h.
Referenced by operator=(), operator=(), refresh(), and source().
|
private |
Definition at line 590 of file thread_safe_container.h.
Referenced by refresh(), and refresh_count().
|
private |
Definition at line 589 of file thread_safe_container.h.
Referenced by contains(), for_each(), get(), keys(), operator=(), operator=(), refresh(), and size().