Skip to content

Latest commit

Β 

History

History
233 lines (172 loc) Β· 5.67 KB

File metadata and controls

233 lines (172 loc) Β· 5.67 KB

πŸš€ C++ Systems Playground

A hands-on C++ monorepo designed to practice systems programming fundamentals, including:

  • custom memory allocation
  • in-memory data structures
  • hash tables (chaining + open addressing)
  • caching & eviction
  • RAII + smart pointers
  • lock-free concurrency
  • thread pools & work stealing
  • modular CMake builds
  • logging
  • unit testing

This repository contains multiple realistic projects that build on each other β€” progressing from memory allocators β†’ hash maps β†’ KV stores β†’ caching β†’ smart pointers β†’ concurrency.


πŸ“¦ Modules Overview

Module Description
common/ Shared utilities (logging)
memory_pool/ Fixed-block allocator for efficient small allocations
hash_map/ Custom separate-chaining HashMap (replaces std::unordered_map)
kv_store_chaining/ KV store using chaining + memory_pool
kv_store_linear/ KV store using open addressing + fixed-inline buffers (cache-friendly)
lru_cache/ Modern LRU cache using hash_map + std::list
in_memory_redis/ Redis-style store with TTL, prefix lookup, background sweeper
smart_pointers/ Custom RAII pointers (UniquePtr, SharedPtr, WeakPtr)
lock_free_queue/ MPMC lock-free queue using atomic CAS (Michael–Scott style)
thread_pool/ Work-stealing thread pool with per-thread task queues & futures

Each module:

  • is standalone
  • exports a library target
  • has a demo executable
  • has unit tests
  • avoids dependencies except STL / gtest

πŸ“‹ Module Dependency Graph

common (logging utilities)
  β”œβ”€ memory_pool (allocators)
  β”‚  └─ kv_store_chaining
  β”‚     └─ (application layer)
  β”‚
  β”œβ”€ hash_map (chaining-based)
  β”‚  β”œβ”€ kv_store_chaining
  β”‚  └─ lru_cache
  β”‚     └─ in_memory_redis
  β”‚
  β”œβ”€ kv_store_linear (independent)
  β”œβ”€ smart_pointers (RAII utilities)
  β”œβ”€ lock_free_queue (concurrency)
  └─ thread_pool (concurrency)

Dependency Rules:

  • common ← base layer (no dependencies)
  • Foundation layer ← common only
  • Application layer ← Foundation + others as needed
  • Concurrency (lock_free_queue, thread_pool) ← common only (no cross-dependencies)

🧩 Monorepo Structure

cpp-systems-playground/
β”‚
β”œβ”€β”€ common/
β”œβ”€β”€ memory_pool/
β”œβ”€β”€ hash_map/
β”‚
β”œβ”€β”€ kv_store_chaining/
β”œβ”€β”€ kv_store_linear/
β”‚
β”œβ”€β”€ lru_cache/
β”œβ”€β”€ in_memory_redis/
β”‚
β”œβ”€β”€ smart_pointers/
β”œβ”€β”€ lock_free_queue/
β”œβ”€β”€ thread_pool/
β”‚
└── build/

KV Store Differences

Module Collision Handling Allocation Value Storage Strength
chaining separate chaining pool dynamic flexible & simple
linear probing open addressing contiguous fixed inline fast & cache-efficient

Concurrency Modules

Module Focus / Concept
lock_free_queue atomic CAS, ABA avoidance, MPMC queues
thread_pool work stealing + futures + per-thread queues

βš™οΈ Build Instructions

Configure:

cmake -S . -B build

Build everything:

cmake --build build -j

Sample specific targets:

cmake --build build --target lock_free_queue_demo
cmake --build build --target thread_pool_demo
cmake --build build --target in_memory_redis_demo

πŸ”₯ Run Demos

./build/lock_free_queue/lock_free_queue_demo
./build/thread_pool/work_stealing_thread_pool_demo

Also available:

./build/hash_map/hash_map_demo
./build/lru_cache/lru_cache_demo
./build/smart_pointers/smart_pointers_demo

πŸ§ͺ Unit Testing

Run all:

cd build
ctest --output-on-failure

Examples:

./build/lock_free_queue/lock_free_queue_tests
./build/thread_pool/thread_pool_tests
./build/kv_store_linear/kv_store_linear_tests

🌟 Learning Outcomes

By completing this repo, you’ll understand:

Data Structures

  • hash maps (chaining + open addressing)
  • memory pool allocators
  • KV stores
  • LRU caching

Concurrency

  • lock-free MPMC queues
  • thread pools & futures
  • work-stealing schedulers
  • condition variable wake/sleep
  • atomic CAS operations

Modern C++ Concepts

  • RAII + ownership models
  • templates + type traits
  • std::invoke_result_t
  • std::function, std::packaged_task
  • orphan avoidance & move semantics

🚧 Future Enhancements

Potential next modules:

  • robin-hood hashing
  • PMR-optimized structures
  • persistent backed Redis (WAL)
  • parallel sort / map-reduce
  • Lock-free stack
  • actor runtime on thread pool
  • benchmark suite

🀝 Contributing

Open to:

  • performance improvements
  • lock-free structures
  • thread pool extensions
  • bug reports & tests
  • benchmark additions
  • more allocator patterns

This is a playground β€” explore, break, optimize, and learn.


πŸ“„ License

MIT β€” free to use and modify.