C++ from Basics to Mastery — Part 23
Complete the mastery capstone with builds, tests, sanitizers, and profiling
The articles on this blog are written for teaching purposes. The information may contain mistakes, so verify it against your target compiler and standard library.
Mastery is not code filled with advanced syntax. It is the ability to explain requirements, lifetime, failure, and performance with reproducible evidence. The final project integrates the series into an asynchronous resource-streaming system.
Capstone contract
request → deduplicate → read file → decode → GPU upload → cache
│ │ │
cancel/error worker pool main-thread boundary
The system must meet these requirements:
- Concurrent requests for one resource key share one operation.
- Work stops when every interested client cancels.
- File bytes, decode buffers, and GPU objects have explicit owners.
- No callback touches destroyed state after worker shutdown.
- Error category and retryability are preserved.
- Time, memory, and allocation targets are measured on a representative workload.
Direct dependencies inward
include/streaming/ public contracts, handles, error types
src/core/ state machine and deduplication
src/io/ file-system adapter
src/decode/ image-decoder adapter
src/gpu/ render-thread adapter
tests/ unit, integration, and race tests
benchmarks/ reproducible workloads
The core state machine does not depend directly on a graphics API or file system. It receives small interfaces or constrained types, and tests inject deterministic fakes.
Inline functions and templates in headers must have equivalent definitions in every translation unit. Changing public object layout or virtual-function order may break an ABI, so shared-library boundaries need a version and rebuild policy.
Reproduce failure with tools
GCC and Clang builds can enable sanitizers in dedicated configurations.
-fsanitize=address,undefined -fno-omit-frame-pointer
-fsanitize=thread
AddressSanitizer detects many lifetime and bounds errors, UndefinedBehaviorSanitizer reports several classes of undefined behavior, and ThreadSanitizer helps find data races. ASan and TSan are normally separate builds, and platform support varies.
Required failure tests include empty and corrupt files, completion racing cancellation, duplicate requests, worker shutdown, and allocator exhaustion. Use barriers, fake executors, and controlled callbacks instead of timing tests with sleeps.
Use profiles to test a hypothesis
Record CPU, OS, compiler version and flags, input data, command, repetitions, and raw results. Examine p50 and p95 latency, peak memory, allocation count, and throughput rather than one average.
Select one measured bottleneck, change one factor, then compare correctness and resources again. std::move, custom allocators, and lock-free structures need evidence, not prestige.
Completion criteria
- Explain ownership and thread affinity with a diagram.
- Give every operation success, failure, cancellation, and shutdown paths.
- Exercise races and lifetime faults with automated tests and sanitizers.
- Reproduce configure, build, and test from a clean environment.
- Record before-and-after measurements and rejected alternatives in ADRs.
- Document unsupported guarantees and the condition for revisiting them.
Mastering C++ does not mean memorizing every language corner. It means expressing value and resource lifetime in types, controlling concurrency and failure boundaries, and revising designs with evidence from compilers, tests, sanitizers, and profilers.