Skip to content

SiddiqSoft/rwlcontainer

Repository files navigation

RWLContainer : Reader-writer protected containers

Build Status

Overview

This library provides convenient, production-ready wrappers around C++20 standard library synchronization primitives:

  • RWLContainer: A reader-writer lock protected dictionary that wraps std::unordered_map with std::shared_mutex for safe concurrent access
  • WaitableQueue: A thread-safe queue that augments std::queue with std::shared_mutex and std::counting_semaphore for efficient producer-consumer patterns

These classes eliminate boilerplate synchronization code and provide a simple, type-safe API for common concurrent programming scenarios.

Requirements

Platform Support

Tested and built on:

  • Windows (Visual Studio 2022)
  • Linux (Fedora)
  • macOS

All platforms are tested in the CI/CD pipeline with comprehensive test coverage.


Classes and Methods

RWLContainer

A thread-safe dictionary with reader-writer locking for efficient concurrent access.

RWLContainer<KeyType, StorageType>
├── Configuration
│   ├── ReplaceExisting
│   └── FailOnCollission
├── Methods
│   ├── add(key, value)
│   ├── add(key, shared_ptr)
│   ├── add(key, callback)
│   ├── remove(key)
│   ├── find(key)
│   ├── size()
│   ├── scan(callback)
│   └── toJson()
└── Properties
    ├── addCounter
    └── removeCounter

RWLContainer Methods

Method Description Link
add(key, StorageType&&) Add element by moving value Details
add(key, StorageTypePtr&&) Add element via shared_ptr Details
add(key, callback) Add element via callback Details
remove(key) Remove and return element Details
find(key) Find element without removing Details
size() Get number of elements Details
scan(callback) Iterate and find elements Details
toJson() Serialize to JSON Details

WaitableQueue

A thread-safe queue with timeout-based waiting for producer-consumer patterns.

WaitableQueue<StorageType>
├── Methods
│   ├── push(value)
│   ├── emplace(value)
│   ├── tryWaitItem(timeout)
│   ├── waitUntilEmpty(timeout)
│   ├── size()
│   ├── addCounter()
│   ├── removeCounter()
│   └── toJson()
└── Properties
    ├── Deleted: copy constructor
    ├── Deleted: move constructor
    ├── Deleted: copy assignment
    └── Deleted: move assignment

WaitableQueue Methods

Method Description Link
push(StorageType&&) Add element to queue Details
emplace(StorageType&&) Construct element in-place Details
tryWaitItem(timeout) Wait for element with timeout Details
waitUntilEmpty(timeout) Wait until queue is empty Details
size() Get number of elements Details
addCounter() Get total adds Details
removeCounter() Get total removes Details
toJson() Serialize to JSON Details

API Documentation

For detailed API documentation, method signatures, and comprehensive examples, see API.md and some notes on tests.

Usage

Quick Example: RWLContainer

#include "siddiqsoft/RWLContainer.hpp"

int main() {
    siddiqsoft::RWLContainer<std::string, int> cache;
    
    // Add items
    cache.add("count", 42);
    cache.add("value", 100);
    
    // Find items
    if (auto val = cache.find("count")) {
        std::cout << "Found: " << *val << std::endl;
    }
    
    // Remove items
    cache.remove("count");
    
    std::cout << "Size: " << cache.size() << std::endl;
    
    return 0;
}

Quick Example: WaitableQueue

#include "siddiqsoft/WaitableQueue.hpp"
#include <thread>

int main() {
    siddiqsoft::WaitableQueue<std::string> queue;
    
    // Producer thread
    std::thread producer([&queue]() {
        for (int i = 0; i < 5; ++i) {
            queue.push(std::string("item_") + std::to_string(i));
        }
    });
    
    // Consumer thread
    std::thread consumer([&queue]() {
        while (true) {
            auto item = queue.tryWaitItem(std::chrono::milliseconds(500));
            if (item) {
                std::cout << "Processed: " << *item << std::endl;
            } else {
                break;
            }
        }
    });
    
    producer.join();
    consumer.join();
    
    return 0;
}

Versioning

Uses GitVersion for semantic versioning (MAJOR.MINOR.PATCH). Version is automatically determined from git history.


© 2021 Siddiq Software LLC. All rights reserved.

About

Small thread-safe read-writer locked container support class.

Topics

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors