m03-mutability

Design C++ classes with const-correct mutable state guarded by mutexes.

14|3|Updated Jan 25, 2026
One-click install
npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m03-mutability-13eholder
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: m03-mutability
Source: https://github.com/13eholder/Modern-Cpp-Skills/tree/main/m03-mutability
Command: npx skills add https://github.com/13eholder/Modern-Cpp-Skills --skill m03-mutability-13eholder

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This skill teaches how to enforce const correctness and manage mutable state in C++ to prevent data races and preserve logical immutability, including locking strategies and proper use of mutable members.

Core Features & Use Cases

  • API Contracts: const methods guarantee no visible state modification.
  • Mutable State Management: design patterns for mutable caches guarded by synchronization primitives.
  • Thread-Safety Guidance: guidelines for when to use std::mutex or std::shared_mutex in API surfaces.
  • Use Case: implement a thread-safe cache inside a const method to balance performance and safety.

Quick Start

Construct a minimal class that demonstrates a const method safely accessing a mutable cache protected by a mutex.

Frequently Asked Questions about m03-mutability

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I maintain const correctness in C++ when a const method needs to cache data?

Use the mutable keyword for cache members and guard them with std::mutex. This allows const methods to modify internal cached state safely without breaking the logical immutability expected by the API contract.

What is the best way to make a C++ class thread-safe while preserving logical immutability?

Apply const correctness to API surfaces and protect mutable state with std::shared_mutex. This synchronization strategy allows multiple concurrent read operations or exclusive write access to internal class data.

When should I use std::shared_mutex instead of std::mutex for C++ thread safety?

Use std::shared_mutex for C++ thread safety when a class has frequent concurrent read operations but infrequent writes. std::mutex is appropriate when operations modify state frequently and require strictly exclusive locking.

Why does making a C++ method const not guarantee thread safety?

A const method in C++ only guarantees no visible modification of logical state, not thread safety. Multiple threads calling a const method can still cause data races if it internally modifies mutable members without synchronization primitives.

Can I use mutable members in C++ without causing data races in multi-threaded components?

Yes, you can use mutable members in multi-threaded components without data races by protecting them with synchronization primitives like std::mutex. This ensures any internal state modification within const methods remains thread-safe.