C++高性能并行编程与优化 - 课件 - 02 现代 C++ 入门:RAII 内存管理# 现代 C++ 入门:RAII 内存管 理 by 彭于斌 (github@archibate) 往期录播:https://space.bilibili.com/263032155 PPT 和代码:https://github.com/parallel101/course ## 高性能并行编程与优化 - 课程大纲 • 分为前半段和后半段,前半段主要介绍现代 C++,后半段主要介绍并行编程与优化。 C++,后半段主要介绍并行编程与优化。 1. 课程安排与开发环境搭建:cmake 与 git 入门 2. 现代 C++ 入门:常用 STL 容器,RAII 内存管理 3. 现代 C++ 进阶:模板元编程与函数式编程 4. 编译器如何自动优化:从汇编角度看 C++ 5. C++11 起的多线程编程:从 mutex 到无锁并行 6. 并行编程常用框架:OpenMP 与 Intel TBB 7. 被忽视的访存优化:内存带宽与 个文件时!  ## C++ 思想:RAII(Resource Acquisition Is Initialization) 资源获取视为初始化,反之,资源释放视为销毁 0.cpp #include#include 0 码力 | 96 页 | 16.28 MB | 2 年前3
Coroutine Patterns and How to Use Them: Problems and Solutions Using Coroutines in a Modern Codebasemakers.f.dev@gmail.com ## Outline • Motivation • Overview • Patterns • Lifetime • Exceptions • RAII • Synchronization ## • Conclusions ## Motivation • 40 years of • Experience • Accumulated knowledge nts/e/2/0/8/e20839ae14710dcbb38ddda7e6a7f799/p14_1.jpg) ## Patterns • Lifetime • Exceptions • RAII • Synchronization ## Lifetime ## Lifetime • Lots of worries • Use Structured Concurrency $ ^{[1]} and members • Automatic variables ## RAII – Classes Async cleanup pattern • Define async cleanup method • Parents/owners call the method, recursively ## RAII – Classes class Foo { Task<>0 码力 | 70 页 | 1.45 MB | 1 年前3
Back to Basics: Smart PointersPointers ■ Allocate und deallocate their resource in the constructor and destructor according to the RAII idiom (Resource Acquisition Is Initialization) ■ Support automatic memory management with reference0 码力 | 30 页 | 625.43 KB | 1 年前3
C++高性能并行编程与优化 - 课件 - 05 C++11 开始的多线程编程• 分为前半段和后半段,前半段主要介绍现代 C++,后半段主要介绍并行编程与优化。 1. 课程安排与开发环境搭建:cmake 与 git 入门 2. 现代 C++ 入门:常用 STL 容器,RAII 内存管理 3. 现代 C++ 进阶:模板元编程与函数式编程 4. 编译器如何自动优化:从汇编角度看 C++ 5. C++11 起的多线程编程:从 mutex 到无锁并行 6. 并行编程常用框架:OpenMP 会用到第二讲(RAII 与智能指针)里的知识 2. 课件中一部分代码是基于 C++17 的  个人认为,C++11中很多特性,其实可以看做是为了支持多线程而顺带引入的……如 chrono、移动、lambda、RAII…… ## 第 0 bb4317bc7805d63cb/p18_1.jpg) ## std::thread 的解构函数会销毁线程 - 作为一个 C++ 类,std::thread 同样遵循 RAII 思想和三五法则:因为管理着资源,他自定义了解构函数,删除了拷贝构造 / 赋值函数,但是提供了移动构造 / 赋值函数。 - 因此,当 t1 所在的函数退出时,就会调用 std::thread 的解构函数,这会销毁0 码力 | 79 页 | 14.11 MB | 2 年前3
Back to Basics: ExceptionsResource Acquisition Is Initialization (RAII) RAII (Resource Acquisition Is Initialization) ## Resource Acquisition Is Initialization (RAII) ## Back to Basics: RAII and the Rule of Zero  "Keep your resources on a short operator=(Widget const& w) { if(this == &w) return *this; // RAII-based approach Widget tmp(w); return *this; } } // ... };0 码力 | 111 页 | 4.87 MB | 1 年前3
CppCon2021 Concurrency TS2 users of this (more on this later) • Non-intrusive rcu_retire() (now in Linux kernel...) ## RCU RAII Readers ## • As C++ developers might expect: void an_rcu_reader() { do_something_before_reader(); under the Creative Commons Attribution-ShareAlike license versions 3.0, 2.5, 2.0, and 1.0. ## RCU RAII Readers • As C++ developers might expect, but more succinctly: void an_rcu_reader() { do_so ; } - Except that not all the world can live within the confines of an RCU RAII reader... ## RCU Non-RAII Readers • And another fine example of diagnostic-driven development! • Function0 码力 | 58 页 | 1.62 MB | 1 年前3
Thinking Functionally In C++{ return value < other; } private: const int value; } UnspeakableLambda l(value); 1(4); ## RAII with lambdas auto object = []() { struct S { S() { puts("constructor"); } ~S() return S(); }; int main() { auto obj = object(); } Program returned: 0 constructor destructor ## RAII with lambdas int main() { auto obj = []() { struct S { S() { puts("constructor"); destructor ## RAII with lambdas auto RAII = [ obj = []() { struct S { S() { puts("constructor"); } ~S() { puts("destructor"); } }; return S(); }()] ## RAII with lambdas0 码力 | 114 页 | 3.14 MB | 1 年前3
Back to Basics: Designing Classes (part 1 of 2)Resource Management C++’s most important idiom: RAII (Resource Acquisition Is Initialization) ▶ ▶ ▶ ▶ ## Resource Management ## Back to Basics: RAII and the Rule of Zero ### Cppcon | 2019 The C++ Conference Arthur O'Dwyer 2019-09-17 Back to Basics: RAII and the Rule of Zero Video Sponsorship Provided By: ## Back to Basics: Smart Pointers and RAII ## I NBAL LEVI Thursday, October 28th, 3:15pm MDT public: // ... Core Guideline R.1: Manage resources automatically using resource handles and RAII (Resource Acquisition Is Initialization) Guideline: Strive for the Rule of 0: Classes that don’t0 码力 | 87 页 | 5.64 MB | 1 年前3
Delivering safe C++(potential range error) ## • Historical factoid • My original design was for readonly and writeonly ## RAII (1979 and later) ## • From my 1979 lab book: • A “new function” creates the run-time environment // release }  ## RAII (Resource Acquisition Is Initialization) // use an object to represent a resource class File_handle Exceptions • Guaranteed error-handling – or termination • Proper interaction with resource management (RAII) ## • Containers • No need to fiddle with arrays (and pointers) • Enable range checking ## • Algorithms0 码力 | 74 页 | 2.72 MB | 1 年前3
Back To Basics: The Rule of Fivestring(data_.get(), cp); } std::unique_ptrdata_; }; ## Simple String - Second RAII Copy Constructor struct SString { String(SString const & rhs) : data_(new char[string(rhs data_.get()); } std::unique_ptrdata_; }; ## Simple String - Second RAII Copy Assignment struct SString { String & operator=(String const & rhs) { data_ String - Second RAII Move Constructor struct SString { String(SString && rhs) noexcept = default; std::unique_ptrdata_; }; ## Simple String - Second RAII Move Assignment0 码力 | 42 页 | 623.10 KB | 1 年前3
共 87 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
相关搜索词
RAII智能指针内存管理构造函数解构函数coroutine patternssynchronizationexception handlinglifetime managementstd::unique_ptrstd::shared_ptrstd::weak_ptrC++20C++11多线程编程std::thread竞态条件内存模型异常错误代码双接口异常安全std::terminateConcurrency TS2hazard ptrRCUshared_ptratomic_shared_ptr函数式编程多范式语言ActionsCalculationsData类设计可读性可变性可测试性资源管理类型安全资源安全配置文件核心指南安全性Rule of FiveDestructorCopy ConstructorMove ConstructorC++ Core Guidelines













