Skip to main content

memory-management-and-concurrency

Explores essential software engineering principles for C/C++, focusing on memory management techniques, CMake usage, RAII, and concurrency primitives to build robust applications.

跳到安装

来源信息

仓库
paulpas/agent-skill-router
最近来源活动
2026年6月10日 18:00
检测到的 SKILL.md 语言
英语
星标
4
分支
1

安装方式

默认使用会先检查来源的 Prompt;你也可以切换为直接命令,或下载本地副本。

检查来源文件

决定是否安装前,请先阅读 SKILL.md,以及 SkillsMP 当前展示的配套文件。

正在显示 SKILL.md

SKILL.md
来源说明 · 只读预览
name
memory-management-and-concurrency
description
Explores essential software engineering principles for C/C++, focusing on memory management techniques, CMake usage, RAII, and concurrency primitives to build robust applications.
license
MIT
compatibility
opencode
metadata
{"version":"1.0.0","domain":"coding","triggers":"memory management, smart pointers, RAII, CMake, concurrency, multithreading, thread safety, synchronization primitives","archetypes":["tactical","generation"],"anti_triggers":["brainstorming","vague ideation","code golf","over-engineering"],"response_profile":{"verbosity":"low","directive_strength":"high","abstraction_level":"operational"},"role":"implementation","scope":"implementation","output-format":"code","related-skills":"coding-testing, coding-debugging, coding-performance-optimization"}
# Memory Management and Concurrency for C/C++ archetypes: tactical, educational anti_triggers: basic memory management response_profile: verbosity: medium directive_strength: high abstraction_level: tactical Explores essential memory management techniques, CMake configuration, RAII, and concurrency patterns crucial for building efficient, maintainable C/C++ applications. ## TL;DR Checklist - [ ] Use smart pointers for RAII to manage memory automatically. - [ ] Employ CMake for cross-platform builds and dependency management. - [ ] Implement thread safety using mutexes or locks in concurrent situations. - [ ] Validate all inputs and handle exceptions for robust error management. ## Core Workflow 1. **Implement Memory Management** — Utilize smart pointers to prevent memory leaks and dangling pointers. **Checkpoint:** Ensure all dynamically allocated resources are managed with `std::shared_ptr` or `std::unique_ptr`. 2. **Configure CMake** — Set up a CMakeLists.txt file to manage project dependencies and build process. **Checkpoint:** Validate that all required libraries are linked correctly and the build configuration is correct. 3. **Utilize RAII** — Create classes that ensure resources are released when they go out of scope. **Checkpoint:** Verify classes have constructors and destructors managing resource allocation and deallocation. 4. **Implement Concurrency** — Use threads and synchronization primitives to manage concurrent access to shared resources. **Checkpoint:** Ensure no data races occur by validating the correct implementation of mutexes or locks. ## Implementation Patterns / Reference Guide ### Additional Implementation Examples 1. **Using Smart Pointers**: Smart pointers ensure automatic memory management. ```cpp #include <iostream> #include <memory> class Resource { public: Resource() { std::cout << "Resource allocated" << std::endl; } ~Resource() { std::cout << "Resource deallocated" << std::endl; } }; void useResource() { std::shared_ptr<Resource> res = std::make_shared<Resource>(); // Resource will be deallocated automatically. } ``` 2. **CMake Setup**: Here's how to utilize CMake effectively in your projects: ```cmake cmake_minimum_required(VERSION 3.10) project(MyProject) set(CMAKE_CXX_STANDARD 11) add_executable(MyExecutable main.cpp) find_package(SomeLibrary REQUIRED) target_link_libraries(MyExecutable PRIVATE SomeLibrary::SomeLibrary) ``` 3. **Concurrency Example**: Managing shared resources: ```cpp #include <iostream> #include <thread> #include <mutex> std::mutex mtx; int sharedResource = 0; void increment() { mtx.lock(); ++sharedResource; mtx.unlock(); } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Shared Resource: " << sharedResource << std::endl; return 0; } ``` ### Additional Concepts - Provide a detailed explanation of RAII. - Discuss threading models and their importance in concurrent programming. ### Pattern 1: Smart Pointers for RAII Using smart pointers ensures that allocated memory is automatically deallocated when no longer in use. This pattern prevents memory leaks common in C/C++ programming. ```cpp #include <iostream> #include <memory> class Resource { public: Resource() { std::cout << "Resource allocated" << std::endl; } ~Resource() { std::cout << "Resource deallocated" << std::endl; } }; void useResource() { std::shared_ptr<Resource> res = std::make_shared<Resource>(); // Resource will be deallocated automatically. } ``` ### Pattern 2: CMake Configuration CMake provides a flexible way to manage project builds and dependencies. Here’s an example of a simple CMakeLists.txt: ```cmake cmake_minimum_required(VERSION 3.10) project(MyProject) set(CMAKE_CXX_STANDARD 11) add_executable(MyExecutable main.cpp) find_package(SomeLibrary REQUIRED) target_link_libraries(MyExecutable PRIVATE SomeLibrary::SomeLibrary) ``` ### Pattern 3: Concurrency with Mutexes In multithreading, protecting shared resources with mutexes ensures thread safety. Here’s an example: ```cpp #include <iostream> #include <thread> #include <mutex> std::mutex mtx; int sharedResource = 0; void increment() { mtx.lock(); ++sharedResource; mtx.unlock(); } int main() { std::thread t1(increment); std::thread t2(increment); t1.join(); t2.join(); std::cout << "Shared Resource: " << sharedResource << std::endl; return 0; } ``` ## Constraints ### MUST DO - Use smart pointers in all dynamic memory management scenarios. - Maintain a clean separation of project files with CMake configurations. - Validate inputs to ensure robust error handling. ### MUST NOT DO - Rely on manual memory management without using smart pointers. - Leave resources allocated in any error path, leading to leaks or exceptions. - Overlook testing concurrency aspects of applications in multi-threaded developments. ## Live References > Authoritative documentation links for this skill's domain. The model follows markdown links at load time to resolve external references and inline content. - [Wikipedia: Memory Management](https://en.wikipedia.org/wiki/Memory_management) - [Preshing on Programming — Memory Allocators Demystified](https://preshing.com/20121224/how-to-demonstrate-the-nuts-and-bolts-of-memory-allocators/) - [C++ Core Guidelines — RAII and Smart Pointers](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#S-resource) - [ISO C++ Standard — Concurrency Support Library](https://en.cppreference.com/w/cpp/thread) - [CMake Official Documentation](https://cmake.org/documentation/)
在 GitHub 查看