Container System 0.1.0
High-performance C++20 type-safe container framework with SIMD-accelerated serialization
Loading...
Searching...
No Matches
kcenon::container::auto_refresh_reader Class Reference

Auto-refreshing lock-free reader. More...

#include <thread_safe_container.h>

Collaboration diagram for kcenon::container::auto_refresh_reader:
Collaboration graph

Public Member Functions

 auto_refresh_reader (std::shared_ptr< thread_safe_container > container, std::chrono::milliseconds refresh_interval)
 Construct auto-refresh reader.
 
 ~auto_refresh_reader ()
 Destructor - stops the refresh thread.
 
 auto_refresh_reader (const auto_refresh_reader &)=delete
 
auto_refresh_readeroperator= (const auto_refresh_reader &)=delete
 
 auto_refresh_reader (auto_refresh_reader &&)=delete
 
auto_refresh_readeroperator= (auto_refresh_reader &&)=delete
 
void stop ()
 Stop the auto-refresh thread.
 
bool is_running () const noexcept
 Check if auto-refresh is running.
 
std::chrono::milliseconds refresh_interval () const noexcept
 Get the refresh interval.
 
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.
 
template<typename Func >
void for_each (Func &&func) const
 Iterate over snapshot.
 
void refresh ()
 Manual refresh (in addition to automatic)
 
size_t refresh_count () const noexcept
 Get the number of refreshes performed.
 
std::shared_ptr< lockfree_container_readerreader () const noexcept
 Get the underlying lock-free reader.
 
std::shared_ptr< thread_safe_containersource () const noexcept
 Get the source container.
 

Private Member Functions

void refresh_loop ()
 

Private Attributes

std::shared_ptr< lockfree_container_readerreader_
 
std::chrono::milliseconds refresh_interval_
 
std::thread refresh_thread_
 
std::mutex mutex_
 
std::condition_variable cv_
 
bool running_
 

Detailed Description

Auto-refreshing lock-free reader.

This class wraps a lockfree_container_reader and automatically refreshes its snapshot at a configurable interval using a background thread. All read operations are delegated to the underlying lock-free reader.

Properties:

  • Read: Wait-free O(1) - same as lockfree_container_reader
  • Refresh: Automatic in background thread
  • Staleness: Bounded by refresh_interval
auto container = std::make_shared<thread_safe_container>();
auto reader = std::make_shared<auto_refresh_reader>(container, std::chrono::milliseconds(100));
// Lock-free reads automatically refreshed every 100ms
auto value = reader->get<int>("counter");
// When done, stop will be called automatically in destructor
std::shared_ptr< lockfree_container_reader > reader() const noexcept
Get the underlying lock-free reader.
Enhanced type-safe value with perfect legacy compatibility.
Definition value.h:129
std::optional< T > get() const
Type-safe getter with optional return.
Definition value.h:213

Definition at line 615 of file thread_safe_container.h.

Constructor & Destructor Documentation

◆ auto_refresh_reader() [1/3]

kcenon::container::auto_refresh_reader::auto_refresh_reader ( std::shared_ptr< thread_safe_container > container,
std::chrono::milliseconds refresh_interval )
inlineexplicit

Construct auto-refresh reader.

Parameters
containerThe thread-safe container to read from
refresh_intervalThe interval between automatic refreshes

Definition at line 622 of file thread_safe_container.h.

624 : reader_(std::make_shared<lockfree_container_reader>(std::move(container)))
626 , running_(true)
627 {
629 }
std::chrono::milliseconds refresh_interval() const noexcept
Get the refresh interval.
std::shared_ptr< lockfree_container_reader > reader_

References refresh_loop(), and refresh_thread_.

Here is the call graph for this function:

◆ ~auto_refresh_reader()

kcenon::container::auto_refresh_reader::~auto_refresh_reader ( )
inline

Destructor - stops the refresh thread.

Definition at line 634 of file thread_safe_container.h.

634 {
635 stop();
636 }
void stop()
Stop the auto-refresh thread.

References stop().

Here is the call graph for this function:

◆ auto_refresh_reader() [2/3]

kcenon::container::auto_refresh_reader::auto_refresh_reader ( const auto_refresh_reader & )
delete

◆ auto_refresh_reader() [3/3]

kcenon::container::auto_refresh_reader::auto_refresh_reader ( auto_refresh_reader && )
delete

Member Function Documentation

◆ contains()

bool kcenon::container::auto_refresh_reader::contains ( std::string_view key) const
inlinenodiscardnoexcept

Lock-free value existence check.

Parameters
keyThe key to check
Returns
true if key exists in snapshot

Definition at line 702 of file thread_safe_container.h.

702 {
703 return reader_->contains(key);
704 }

References reader_.

◆ empty()

bool kcenon::container::auto_refresh_reader::empty ( ) const
inlinenodiscardnoexcept

Lock-free empty check.

Returns
true if snapshot is empty

Definition at line 718 of file thread_safe_container.h.

718 {
719 return reader_->empty();
720 }

References reader_.

◆ for_each()

template<typename Func >
void kcenon::container::auto_refresh_reader::for_each ( Func && func) const
inline

Iterate over snapshot.

Template Parameters
FuncCallback function type
Parameters
funcCallback to apply to each key-value pair

Definition at line 736 of file thread_safe_container.h.

736 {
737 reader_->for_each(std::forward<Func>(func));
738 }

References reader_.

◆ get()

template<concepts::ValueVariantType T>
std::optional< T > kcenon::container::auto_refresh_reader::get ( std::string_view key) const
inlinenodiscardnoexcept

Lock-free typed value read.

Delegates to the underlying lockfree_container_reader.

Template Parameters
TThe expected value type
Parameters
keyThe key to look up
Returns
Optional containing value if found and type matches

Definition at line 693 of file thread_safe_container.h.

693 {
694 return reader_->get<T>(key);
695 }

References reader_.

◆ is_running()

bool kcenon::container::auto_refresh_reader::is_running ( ) const
inlinenodiscardnoexcept

Check if auto-refresh is running.

Returns
true if auto-refresh thread is active

Definition at line 670 of file thread_safe_container.h.

670 {
671 std::lock_guard<std::mutex> lock(mutex_);
672 return running_;
673 }

References mutex_, and running_.

◆ keys()

std::vector< std::string > kcenon::container::auto_refresh_reader::keys ( ) const
inlinenodiscard

Get all keys from snapshot.

Returns
Vector of keys in the current snapshot

Definition at line 726 of file thread_safe_container.h.

726 {
727 return reader_->keys();
728 }

References reader_.

◆ operator=() [1/2]

auto_refresh_reader & kcenon::container::auto_refresh_reader::operator= ( auto_refresh_reader && )
delete

◆ operator=() [2/2]

auto_refresh_reader & kcenon::container::auto_refresh_reader::operator= ( const auto_refresh_reader & )
delete

◆ reader()

std::shared_ptr< lockfree_container_reader > kcenon::container::auto_refresh_reader::reader ( ) const
inlinenodiscardnoexcept

Get the underlying lock-free reader.

Returns
Shared pointer to the underlying reader

Definition at line 761 of file thread_safe_container.h.

761 {
762 return reader_;
763 }

References reader_.

◆ refresh()

void kcenon::container::auto_refresh_reader::refresh ( )
inline

Manual refresh (in addition to automatic)

Forces an immediate refresh of the snapshot.

Definition at line 745 of file thread_safe_container.h.

745 {
746 reader_->refresh();
747 }

References reader_.

◆ refresh_count()

size_t kcenon::container::auto_refresh_reader::refresh_count ( ) const
inlinenodiscardnoexcept

Get the number of refreshes performed.

Returns
Total refresh count (automatic + manual)

Definition at line 753 of file thread_safe_container.h.

753 {
754 return reader_->refresh_count();
755 }

References reader_.

◆ refresh_interval()

std::chrono::milliseconds kcenon::container::auto_refresh_reader::refresh_interval ( ) const
inlinenodiscardnoexcept

Get the refresh interval.

Returns
The configured refresh interval

Definition at line 679 of file thread_safe_container.h.

679 {
680 return refresh_interval_;
681 }

References refresh_interval_.

◆ refresh_loop()

void kcenon::container::auto_refresh_reader::refresh_loop ( )
inlineprivate

Definition at line 774 of file thread_safe_container.h.

774 {
775 while (true) {
776 std::unique_lock<std::mutex> lock(mutex_);
777 if (cv_.wait_for(lock, refresh_interval_, [this] { return !running_; })) {
778 break; // Stopped
779 }
780 lock.unlock();
781 reader_->refresh();
782 }
783 }

References cv_, mutex_, reader_, and refresh_interval_.

Referenced by auto_refresh_reader().

Here is the caller graph for this function:

◆ size()

size_t kcenon::container::auto_refresh_reader::size ( ) const
inlinenodiscardnoexcept

Lock-free snapshot size check.

Returns
Number of values in the current snapshot

Definition at line 710 of file thread_safe_container.h.

710 {
711 return reader_->size();
712 }

References reader_.

◆ source()

std::shared_ptr< thread_safe_container > kcenon::container::auto_refresh_reader::source ( ) const
inlinenodiscardnoexcept

Get the source container.

Returns
Shared pointer to the source container

Definition at line 769 of file thread_safe_container.h.

769 {
770 return reader_->source();
771 }

References reader_.

◆ stop()

void kcenon::container::auto_refresh_reader::stop ( )
inline

Stop the auto-refresh thread.

After calling stop(), no more automatic refreshes will occur. Manual refresh() calls are still possible.

Definition at line 652 of file thread_safe_container.h.

652 {
653 {
654 std::lock_guard<std::mutex> lock(mutex_);
655 if (!running_) {
656 return;
657 }
658 running_ = false;
659 }
660 cv_.notify_one();
661 if (refresh_thread_.joinable()) {
662 refresh_thread_.join();
663 }
664 }

References cv_, mutex_, refresh_thread_, and running_.

Referenced by ~auto_refresh_reader().

Here is the caller graph for this function:

Member Data Documentation

◆ cv_

std::condition_variable kcenon::container::auto_refresh_reader::cv_
private

Definition at line 789 of file thread_safe_container.h.

Referenced by refresh_loop(), and stop().

◆ mutex_

std::mutex kcenon::container::auto_refresh_reader::mutex_
mutableprivate

Definition at line 788 of file thread_safe_container.h.

Referenced by is_running(), refresh_loop(), and stop().

◆ reader_

std::shared_ptr<lockfree_container_reader> kcenon::container::auto_refresh_reader::reader_
private

◆ refresh_interval_

std::chrono::milliseconds kcenon::container::auto_refresh_reader::refresh_interval_
private

Definition at line 786 of file thread_safe_container.h.

Referenced by refresh_interval(), and refresh_loop().

◆ refresh_thread_

std::thread kcenon::container::auto_refresh_reader::refresh_thread_
private

Definition at line 787 of file thread_safe_container.h.

Referenced by auto_refresh_reader(), and stop().

◆ running_

bool kcenon::container::auto_refresh_reader::running_
private

Definition at line 790 of file thread_safe_container.h.

Referenced by is_running(), and stop().


The documentation for this class was generated from the following file: