RWLEnvelope 0.0.0
Thread-safe envelope wrapper using reader-writer locks
Loading...
Searching...
No Matches
siddiqsoft::RWLEnvelope< ContainerType > Class Template Reference

Thread-safe envelope wrapper using reader-writer locks. More...

#include </opt/azure-agent/_work/15/s/include/siddiqsoft/RWLEnvelope.hpp>

Public Member Functions

 RWLEnvelope (RWLEnvelope< ContainerType > &&src) noexcept
 RWLEnvelope (ContainerType &&src)
template<typename T>
 RWLEnvelope (std::initializer_list< T > init)
void reassign (ContainerType &&src)
 RWLEnvelope (const ContainerType &arg)
RWLEnvelope & operator= (RWLEnvelope const &)=delete
 Copy assignment operator (deleted).
template<typename Callback, typename... Args>
auto observe (Callback cbf, Args &&... args) const
template<typename Callback, typename... Args>
auto mutate (Callback cbf, Args &&... args)
ContainerType snapshot () const
std::tuple< const ContainerType &, RLock > readLock ()
std::tuple< ContainerType &, RWLock > writeLock ()

Detailed Description

template<typename ContainerType>
requires std::copy_constructible<ContainerType>
class siddiqsoft::RWLEnvelope< ContainerType >

Thread-safe envelope wrapper using reader-writer locks.

Template Parameters
ContainerTypeType for your object which is to be "enveloped"

RWLEnvelope provides a simple, convenient envelope-access model for thread-safe access to objects using reader-writer locks. It wraps a type ContainerType with std::shared_mutex to enable safe concurrent read and exclusive write access patterns.

Thread Safety Guarantees

  • Multiple Readers: Multiple threads can call observe() or readLock() concurrently
  • Exclusive Writer: Only one thread can call mutate() or writeLock() at a time
  • Reader-Writer Exclusion: No readers can access while a writer is active, and vice versa
  • Exception Safe: Move constructor is noexcept; mutate() includes exception handling

Usage Patterns

Callback-based Access

bool exists = cache.observe([](const auto& m) noexcept {
return m.count("key") > 0;
});
cache.mutate([](auto& m) noexcept {
m["key"] = 42;
});
Thread-safe envelope wrapper using reader-writer locks.

Direct Lock Access

if (auto const& [map, lock] = data.readLock(); lock) {
auto it = map.find("key");
if (it != map.end()) {
std::cout << it->second << std::endl;
}
}

Snapshot for External Processing

std::vector<int> copy = data.snapshot();
std::sort(copy.begin(), copy.end());

Callbacks with Additional Arguments

std::string searchKey = "target";
bool found = data.observe(
[](const auto& m, const std::string& key) noexcept {
return m.find(key) != m.end();
},
searchKey
);

Performance Considerations

  • Keep callbacks and lock scopes as short as possible
  • Avoid I/O operations (file, network) within locks
  • Use snapshot() for expensive post-processing
  • Use observe() or readLock() to avoid copying
  • Direct callbacks eliminate std::function overhead

Limitations

  • Copy assignment is deleted; use move semantics
  • Default constructor requires T to be default-constructible
  • Mutex is not shared when moving envelopes
  • No recursive locking (will deadlock)
  • All callbacks must be marked noexcept
Examples
/opt/azure-agent/_work/15/s/include/siddiqsoft/RWLEnvelope.hpp.

Definition at line 180 of file RWLEnvelope.hpp.

Constructor & Destructor Documentation

◆ RWLEnvelope() [1/5]

template<typename ContainerType>
siddiqsoft::RWLEnvelope< ContainerType >::RWLEnvelope ( )
inlineexplicit

Definition at line 221 of file RWLEnvelope.hpp.

◆ RWLEnvelope() [2/5]

template<typename ContainerType>
siddiqsoft::RWLEnvelope< ContainerType >::RWLEnvelope ( RWLEnvelope< ContainerType > && src)
inlineexplicitnoexcept

Definition at line 241 of file RWLEnvelope.hpp.

◆ RWLEnvelope() [3/5]

template<typename ContainerType>
siddiqsoft::RWLEnvelope< ContainerType >::RWLEnvelope ( ContainerType && src)
inlineexplicit

Definition at line 273 of file RWLEnvelope.hpp.

◆ RWLEnvelope() [4/5]

template<typename ContainerType>
template<typename T>
siddiqsoft::RWLEnvelope< ContainerType >::RWLEnvelope ( std::initializer_list< T > init)
inlineexplicit

Definition at line 297 of file RWLEnvelope.hpp.

◆ RWLEnvelope() [5/5]

template<typename ContainerType>
siddiqsoft::RWLEnvelope< ContainerType >::RWLEnvelope ( const ContainerType & arg)
inlineexplicit

Definition at line 338 of file RWLEnvelope.hpp.

Member Function Documentation

◆ mutate()

template<typename ContainerType>
template<typename Callback, typename... Args>
auto siddiqsoft::RWLEnvelope< ContainerType >::mutate ( Callback cbf,
Args &&... args )
inline

Definition at line 415 of file RWLEnvelope.hpp.

◆ observe()

template<typename ContainerType>
template<typename Callback, typename... Args>
auto siddiqsoft::RWLEnvelope< ContainerType >::observe ( Callback cbf,
Args &&... args ) const
inline

Definition at line 377 of file RWLEnvelope.hpp.

◆ operator=()

template<typename ContainerType>
RWLEnvelope & siddiqsoft::RWLEnvelope< ContainerType >::operator= ( RWLEnvelope< ContainerType > const & )
delete

Copy assignment operator (deleted).

Copy assignment is explicitly deleted to enforce move semantics and prevent accidental expensive copies.

Note
Use move semantics or reassign() instead
Examples
/opt/azure-agent/_work/15/s/include/siddiqsoft/RWLEnvelope.hpp.

◆ readLock()

template<typename ContainerType>
std::tuple< const ContainerType &, RLock > siddiqsoft::RWLEnvelope< ContainerType >::readLock ( )
inlinenodiscard

Definition at line 478 of file RWLEnvelope.hpp.

◆ reassign()

template<typename ContainerType>
void siddiqsoft::RWLEnvelope< ContainerType >::reassign ( ContainerType && src)
inline

Definition at line 317 of file RWLEnvelope.hpp.

◆ snapshot()

template<typename ContainerType>
ContainerType siddiqsoft::RWLEnvelope< ContainerType >::snapshot ( ) const
inlinenodiscard

Definition at line 447 of file RWLEnvelope.hpp.

◆ writeLock()

template<typename ContainerType>
std::tuple< ContainerType &, RWLock > siddiqsoft::RWLEnvelope< ContainerType >::writeLock ( )
inlinenodiscard

Definition at line 503 of file RWLEnvelope.hpp.


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