Learn Advanced C++: From Concurrency to Coroutines - Expanded Project Guides

Generated from: LEARN_ADVANCED_CPP_DEEP_DIVE.md

Overview

This collection of expanded project guides takes you beyond C++ fundamentals into the advanced features that define modern, high-performance systems programming. You’ll master exception safety, multithreading, template metaprogramming, and C++20 coroutines through hands-on implementation.

These aren’t toy projects—they’re the building blocks of production systems: game engines, trading systems, high-throughput servers, and embedded systems all rely on these techniques.

Why These Topics Matter

Traditional C++                    Modern Advanced C++
┌─────────────────────┐           ┌─────────────────────────────────────┐
│ Manual error codes  │           │ Exception-safe RAII patterns        │
│ Single-threaded     │    →      │ Lock-free concurrent structures     │
│ Runtime type checks │           │ Compile-time type enforcement       │
│ Callback hell       │           │ Clean coroutine-based async         │
└─────────────────────┘           └─────────────────────────────────────┘

Project Index

# Project Difficulty Time Key Focus
1 The Exception-Safe Vector Advanced 1-2 weeks RAII, exception guarantees, copy-and-swap idiom
2 Thread-Safe Producer-Consumer Queue Advanced 1-2 weeks std::mutex, std::condition_variable, race conditions
3 Compile-Time Unit Conversion Library Expert 2-3 weeks Template metaprogramming, type system encoding
4 Coroutine-based Redis Client Master 2-4 weeks C++20 coroutines, async I/O, awaitable design

Learning Paths

Path 1: Exception Safety & Resource Management (Week 1-2)

Start here if you want to write bulletproof C++ code.

  1. P01: Exception-Safe Vector
    • Master RAII and the strong exception guarantee
    • Learn the copy-and-swap idiom
    • Understand noexcept specifications

Path 2: Concurrent Programming (Week 1-4)

Start here if you want to leverage multi-core processors.

  1. P02: Thread-Safe Queue
    • Learn mutex and condition variable patterns
    • Understand race conditions and how to prevent them
    • Build production-quality concurrent data structures

Path 3: Template Metaprogramming (Week 2-5)

Start here if you want to catch errors at compile time.

  1. P01: Exception-Safe Vector (templates foundation)
  2. P03: Compile-Time Units
    • Encode invariants in the type system
    • Perform compile-time computations
    • Zero-overhead abstractions

Path 4: Modern Async C++ (Week 2-6)

Start here if you want to write clean asynchronous code.

  1. P02: Thread-Safe Queue (concurrency foundation)
  2. P04: Coroutine Redis Client
    • Master C++20 coroutine machinery
    • Wrap callback-based I/O in clean interfaces
    • Eliminate callback hell

Complete Mastery Path (6-10 weeks)

For those who want to master all advanced C++ topics:

Week 1-2:  P01 (Exception Safety)
Week 2-3:  P02 (Concurrency Basics)
Week 4-5:  P03 (Template Metaprogramming)
Week 6-10: P04 (Coroutines + Integration)

Prerequisites

Essential

  • Strong C++ fundamentals (classes, inheritance, pointers, references)
  • Memory management (new/delete, stack vs heap)
  • Basic template syntax (function and class templates)
  • Standard library containers (std::vector, std::map)

Helpful but Not Required

  • Move semantics and rvalue references
  • Lambda expressions
  • Smart pointers (std::unique_ptr, std::shared_ptr)
  • Understanding of the C++ compilation model

Self-Assessment Questions

Before starting, you should be able to answer:

  1. What is the difference between stack and heap allocation?
  2. What happens when a destructor is called?
  3. What is a template specialization?
  4. What is the difference between const T& and T&&?
  5. When is a copy constructor called vs a move constructor?

Core Concepts Overview

Exception Handling & RAII

The C++ philosophy: tie resource lifetime to object lifetime.

void process() {
    std::unique_ptr<Resource> r = make_resource();  // Acquired
    do_work(r);
    // If do_work throws, r's destructor still runs
}  // r automatically released here

Concurrency Primitives

Primitive Purpose When to Use
std::thread Execute code in parallel CPU-bound work
std::mutex Protect shared data Any shared state
std::condition_variable Wait for conditions Producer-consumer patterns
std::atomic<T> Lock-free operations Counters, flags
std::future Get async results Task-based parallelism

Template Metaprogramming

Computation at compile time:

// Runtime check (bad)
if (is_pointer(x)) { ... }

// Compile-time check (good)
template<typename T>
void process(T x) {
    if constexpr (std::is_pointer_v<T>) {
        // Only compiled if T is a pointer
    }
}

C++20 Coroutines

Functions that can suspend and resume:

task<int> async_work() {
    co_await network_read();   // Suspends, doesn't block
    co_return compute_result();
}

Essential Books

Book Author Topics Covered
Effective C++, 3rd Ed Scott Meyers Exception safety, RAII
Effective Modern C++ Scott Meyers Move semantics, smart pointers
C++ Concurrency in Action, 2nd Ed Anthony Williams All concurrency topics
C++ Templates: The Complete Guide, 2nd Ed Vandevoorde et al. Template metaprogramming
C++20 - The Complete Guide Nicolai Josuttis Coroutines, concepts, ranges

Development Environment

Compiler Requirements

  • C++17 minimum: Projects 1-3
  • C++20 required: Project 4
  • GCC 10+ (full C++20 coroutine support)
  • Clang 14+ (full C++20 coroutine support)
  • MSVC 2019 16.8+ (C++20 support)

Essential Tools

  • Valgrind or AddressSanitizer: Memory error detection
  • ThreadSanitizer: Race condition detection
  • CMake: Build system
  • GDB/LLDB: Debugging

Quick Setup (Ubuntu/Debian)

sudo apt install g++-11 cmake valgrind gdb

Quick Setup (macOS)

brew install llvm cmake

Project Comparison

Aspect P01 Vector P02 Queue P03 Units P04 Redis
C++ Standard C++17 C++17 C++17 C++20
Memory Focus High Medium Low Medium
Concurrency Low High None High
Templates Medium Low Very High Medium
External Deps None None None Asio
Debugging Difficulty Medium High Medium Very High

Success Criteria

After completing all projects, you should be able to:

  1. Write exception-safe code that never leaks resources
  2. Design thread-safe data structures without race conditions
  3. Encode domain rules in the type system catching errors at compile time
  4. Write asynchronous code that looks synchronous using coroutines
  5. Debug concurrent and asynchronous code effectively
  6. Answer advanced C++ interview questions confidently

Directory Structure

LEARN_ADVANCED_CPP_DEEP_DIVE/
├── README.md                          ← You are here
├── P01-exception-safe-vector.md       ← RAII & exception guarantees
├── P02-thread-safe-queue.md           ← Concurrency fundamentals
├── P03-compile-time-units.md          ← Template metaprogramming
├── P04-coroutine-redis-client.md      ← Modern async C++
└── assets/                            ← Diagrams (generated)

Last updated: 2025-12-29