cpp-thread-safety

Enforces RAII ownership and thread-safe patterns in C++ code with sanitizer verification.

6|2|Updated May 6, 2026
One-click install
npx skills add https://github.com/sek788432/Stock-Back-Test-System --skill cpp-thread-safety-sek788432
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: cpp-thread-safety
Source: https://github.com/sek788432/Stock-Back-Test-System/tree/main/.agents/skills/cpp-thread-safety
Command: npx skills add https://github.com/sek788432/Stock-Back-Test-System --skill cpp-thread-safety-sek788432

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? C++ codebases frequently suffer from data races, deadlocks, and memory leaks caused by raw pointers, manual mutex locking, and unsynchronized shared state. This Skill provides a strict rulebook for writing and reviewing thread-safe, leak-free C++ code in this repository. ## Core Features & Use Cases - RAII Ownership Enforcement: Mandates std::unique_ptr, std::jthread, std::scoped_lock, and rule-of-zero classes while banning raw new/delete, bare mutex.lock(), and std::thread::detach(). - Cross-Thread Communication Rules: Defines immutable snapshot passing, std::atomic usage, std::stop_token cancellation, and Qt-specific queued signal/slot patterns. - Race Pattern Detection: Lists six common race and deadlock anti-patterns to refuse during code review, with corrected replacements. - Use Case: When reviewing a pull request that adds a worker thread communicating with a Qt UI, apply this Skill to verify queued connections, metatype registration, and that no widget is touched off the main thread. ## Quick Start Review my new C++ worker class for thread safety, RAII violations, and sanitizer readiness using the cpp-thread-safety rules.

Frequently Asked Questions about cpp-thread-safety

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

FAQPage Schema
How do I make C++ code thread-safe with mutexes?▼

Use std::scoped_lock for any mutex acquisition, especially when locking multiple mutexes, since it prevents deadlock from inconsistent lock ordering. Keep critical sections short with no I/O or callbacks held under the lock, and document the synchronization protocol above shared fields.

How to pass data between threads in C++ safely?▼

Pass owned values or immutable snapshots across thread boundaries instead of sharing mutable state. For Qt, emit signals with Qt::QueuedConnection and Q_DECLARE_METATYPE-registered argument types, and use QMetaObject::invokeMethod for UI-to-worker calls.

Should I use std::thread or std::jthread in modern C++?▼

Use std::jthread because it auto-joins on destruction and supports cooperative cancellation through std::stop_token. std::thread::detach() is banned since detached threads can outlive the data they reference.

Does volatile prevent data races in C++?▼

No, volatile is not a synchronization primitive in C++ and does not prevent data races. Use std::atomic<T> for shared flags and counters, defaulting to sequential consistency unless a weaker ordering is justified in writing.

How do I detect memory leaks and data races in C++?▼

Build and test with the dev-sanitize preset running AddressSanitizer, LeakSanitizer, and UBSan, then run the separate dev-tsan preset for ThreadSanitizer since ASan and TSan cannot combine. Never suppress sanitizer reports; fix the underlying ownership or synchronization defect.

When is shared mutable state acceptable in C++?▼

Only with a written reason and a documented synchronization protocol specifying who locks what and in what order. Use std::shared_mutex when reads dominate, return values rather than references from locked accessors, and break shared_ptr cycles with std::weak_ptr.