
Quick Start
RWLEnvelope is a header-only C++ template library that provides a simple, convenient envelope-access model for thread-safe access to objects using reader-writer locks.
data.observe([](const auto& m) noexcept {
return m.find("key") != m.end();
});
data.mutate([](auto& m) noexcept {
m["key"] = 42;
});
Thread-safe envelope wrapper using reader-writer locks.
Thread-safe envelope wrapper using reader-writer locks.
Why RWLEnvelope?
- Automatic Lock Management: No manual lock/unlock needed
- Clear Intent: observe() for reads, mutate() for writes
- Exception Safe: Locks released properly even if callbacks throw
- Zero Overhead: Header-only, no runtime cost beyond std::shared_mutex
- Works with Any Type: Not limited to specific containers
Installation
NuGet Package
Install-Package SiddiqSoft.RWLEnvelope
Header-Only
Copy include/siddiqsoft/RWLEnvelope.hpp to your project.
Requirements
- C++ Standard: C++20 or later (requires C++20 concepts support)
- Compiler: Must support [[nodiscard]] attribute and C++20 concepts
- Headers: <shared_mutex>, <functional>, <tuple>, <utility>, <concepts>
- Optional: nlohmann/json library for JSON serialization support
Key Features
Multiple Concurrent Readers
data.observe([](const auto& m) noexcept {
return m.at("key");
});
Exclusive Writer
data.mutate([](auto& m) noexcept {
m["key"] = 42;
});
Direct Lock Access
if (auto [map, lock] = data.writeLock(); lock) {
map["key"] = 42;
map.erase("old_key");
}
Snapshot for External Processing
std::vector<int> copy = data.snapshot();
std::sort(copy.begin(), copy.end());
Real-World Examples
Configuration Management
auto url = config.observe([](const auto& cfg) noexcept {
return cfg.databaseUrl;
});
config.mutate([](auto& cfg) noexcept {
cfg.databaseUrl = "new_url";
});
Cache Implementation
auto val = cache.observe([](const auto& c) noexcept {
return c.at("key").value;
});
cache.mutate([](auto& c) noexcept {
c["key"] = {"computed_value", now()};
});
Testing
The library includes comprehensive test coverage with 38+ tests covering:
- Basic functionality (observe, mutate, readLock, writeLock)
- Edge cases and exception safety
- Concurrent access patterns
- Data integrity under contention
Run tests:
cmake --preset Apple-Debug
cmake --build --preset Apple-Debug
ctest --preset Apple-Debug
License
BSD 3-Clause License - See LICENSE file for details
Resources
© 2021 Abdulkareem Siddiq. All rights reserved.