C++高性能并行编程与优化 - 课件 - 02 现代 C++ 入门:RAII 内存管理现代 C++ 入门: RAII 内存管 理 by 彭于斌( github@archibate ) 往期录播: https://space.bilibili.com/263032155 PPT 和代码: https://github.com/parallel101/course 高性能并行编程与优化 - 课程大纲 • 分为前半段和后半段,前半段主要介绍现代 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 ) 资源获取视为初始化,反之,资源释放视为销毁 C++ 除了用于初始化的构造函数( constructor ) 还包括了用于销毁的解构函数( destructor ) 离开 {} 作用域自动释放 手动释放 RAII :避免犯错误 与 Java , Python0 码力 | 96 页 | 16.28 MB | 1 年前3
Coroutine Patterns and How to Use Them: Problems and Solutions Using Coroutines in a Modern Codebase2023 3 makers.f.dev@gmail.comOutline • Motivation • Overview • Patterns • Lifetime • Exceptions • RAII • Synchronization • Conclusions Francesco Zoffoli - Coroutine Patterns and How to Use Them - CppCon Them - CppCon 2023 16 Queue Thread foo()_1 Executor foo()_2Patterns • Lifetime • Exceptions • RAII • Synchronization Francesco Zoffoli - Coroutine Patterns and How to Use Them - CppCon 2023 17Lifetime - CppCon 2023 50RAII Different solutions for • Class hierarchies and members • Automatic variables Francesco Zoffoli - Coroutine Patterns and How to Use Them - CppCon 2023 51RAII – Classes Async0 码力 | 70 页 | 1.45 MB | 6 月前3
Back to Basics: ExceptionsAcquisition Is Initialization (RAII) 79 RAII (Resource Acquisition Is Initialization)Resource Acquisition Is Initialization (RAII) 80Resource Acquisition Is Initialization (RAII) 81 ”Keep your resources operator Widget& operator=( Widget const& w ) { if( this == &w ) return *this; // RAII-based approach Widget tmp( w ); return *this; } // … }; Exception unsafe operator Widget& operator=( Widget const& w ) { if( this == &w ) return *this; // RAII-based approach Widget tmp( w ); *this = move(tmp); return *this; }0 码力 | 111 页 | 4.87 MB | 6 月前3
C++高性能并行编程与优化 - 课件 - 05 C++11 开始的多线程编程• 分为前半段和后半段,前半段主要介绍现代 C++ ,后半段主要介绍并行编程与优化。 1.课程安排与开发环境搭建: cmake 与 git 入门 2.现代 C++ 入门:常用 STL 容器, RAII 内存管理 3.现代 C++ 进阶:模板元编程与函数式编程 4.编译器如何自动优化:从汇编角度看 C++ 5.C++11 起的多线程编程:从 mutex 到无锁并行 6.并行编程常用框架: CUDA Toolkit 10.0 以上( GPU 专题) 温馨提示: 1. 会用到第二讲( RAII 与智能指针)里的知识 2. 课件中一部分代码是基于 C++17 的 个人认为, C++11 中很多特性, 其实可以看做是为了支持多线程而 顺带引入的……如 chrono 、移动 、 lambda 、 RAII…… 第 0 章:时间 C 语言如何处理时间: time.h • long t0 = 可以用 std::thread 类的成员函数 join() 来等待该进程结束。 std::thread 的解构函数会销毁线程 • 作为一个 C++ 类, std::thread 同样遵循 RAII 思想和三五法则:因为管理着资源, 他自定义了解构函数,删除了拷贝构造 / 赋 值函数,但是提供了移动构造 / 赋值函数。 • 因此,当 t1 所在的函数退出时,就会调用 std::thread0 码力 | 79 页 | 14.11 MB | 1 年前3
Thinking Functionally In C++temperature); OVEN_ERR oven_get_temperature(OVEN_HANDLE handle, int* temperature);Not turning off? RAII to the rescue! typedef int OVEN_HANDLE typedef int OVEN_ERR OVEN_HANDLE oven_reserve_next_available(); oven_get_temperature(mHandle, *temperature); } private: OVEN_HANDLE mHandle; };Not turning off? RAII to the rescue! typedef int OVEN_HANDLE typedef int OVEN_ERR OVEN_HANDLE oven_reserve_next_available(); Oven(oven_reserve_next_available()); oven1.TurnOn(); oven1.SetTemperature(375);Not turning off? RAII to the rescue! typedef int OVEN_HANDLE typedef int OVEN_ERR OVEN_HANDLE oven_reserve_next_available();0 码力 | 114 页 | 3.14 MB | 6 月前3
CppCon2021 Concurrency TS2 replaced museum-piece abstract classes ;-) • synchronize_rcu() to rcu_synchronize() for consistency • RAII: rcu_reader to a Cpp17BasicLockable rcu_domain • Deleters may be invoked directly from a retire call this (more on this later) • Non-intrusive rcu_retire() (now in Linux kernel…) 39 © 2021. 40 RCU RAII Readers • As C++ developers might expect: void an_rcu_reader() { do_something_before_reader(); Attribution-ShareAlike license versions 3.0, 2.5, 2.0, and 1.0. 41 41 As RCU users might expect: © 2021. 42 RCU RAII Readers • As C++ developers might expect, but more succinctly: void an_rcu_reader() { do_0 码力 | 58 页 | 1.62 MB | 6 月前0.03
Back To Basics: The Rule of FiveCompiler-Generated FunctionsCopyright © 2023 Andre KosturSimple String - First RAII pass struct SString { SString(char const * cp) : data_(new char[strlen(cp) + 1]) { strcpy(data_ std::shared_ptr data_; }; 27Copyright © 2023 Andre Kostur Simple String - Second RAII pass struct SString { SString(char const * cp) : data_(new char[strlen(cp) + 1]) { strcpy(data_ std::unique_ptr data_; }; 28Copyright © 2023 Andre Kostur Simple String - Second RAII Copy Constructor struct SString { SString(SString const & rhs) : data_(new char[strlen(rhs 0 码力 | 42 页 | 623.10 KB | 6 月前3
Back to Basics: Designing Classes (part 1 of 2)R.1: Manage resources automatically using resource handles and RAII (Resource Acquisition Is Initialization)Resource Management 76 RAII (Resource Acquisition Is Initialization) C++’s most important 1: Manage resources automatically using resource handles and std::unique_ptr cannot be copied! RAII (Resource Acquisition Is Initialization)Resource Management 80 class Widget { public: // Guideline: Strive for the Rule of 0, but if it cannot be achieved (e.g. because the class implements RAII itself), follow the Rule of 5. Guideline: Design classes for easy change.85 Wednesday, October 27th0 码力 | 87 页 | 5.64 MB | 6 月前3
Back to Basics: ConcurrencyO’Dwyer 2020-09-18Outline ● What is a data race and how do we fix it? [3–12] ● C++11 mutex and RAII lock types [13–23] Questions? ● condition_variable [24–28] ● Static initialization and once_flag unlocked it. We should look for a way to follow RAII principles: Every “cleanup” action, including unlocking mutexes, should be done inside a destructor.RAII to the rescue! Token getToken() { management, just the same as a heap-allocated pointer or a locked mutex. ● So we should have an RAII type for it, right? ● C++20 gives us std::jthread (“joining thread”)... 54Bonus: C++20 std::jthread0 码力 | 58 页 | 333.56 KB | 6 月前3
C++高性能并行编程与优化 - 课件 - 03 现代 C++ 进阶:模板元编程• 分为前半段和后半段,前半段主要介绍现代 C++ ,后半段主要介绍并行编程与优化。 1.课程安排与开发环境搭建: cmake 与 git 入门 2.现代 C++ 入门:常用 STL 容器, RAII 内存管理 3.现代 C++ 进阶:模板元编程与函数式编程 4.编译器如何自动优化:从汇编角度看 C++ 5.C++11 起的多线程编程:从 mutex 到无锁并行 6.并行编程常用框架: has_value()) 等价 。 • 没错,这样看来 optional 是在模仿指针 , nullopt 则模仿 nullptr 。但是他更安 全,且符合 RAII 思想,当设为 nullopt 时会自动释放内部的对象。 • 利用这一点可以实现 RAII 容器的提前 释放。和 unique_ptr 的区别在于他的 对象存储在栈上,效率更高。 variant :安全的 union ,存储多个不同类型的值 有时候需要一个类型“要么存储 int ,要么存储 float” ,这时候就可以用 std::variant。 • 和 union 相比, variant 符合 RAII 思想,更 加安全易用。 • 给 variant 赋值只需用普通的 = 即可。 • variant 的特点是只存储其中一种类型。 • tuple 的特点是每个类型都有存储。 • 请区分,根据实际情况选用适当的容器。 0 码力 | 82 页 | 12.15 MB | 1 年前3
共 49 条
- 1
- 2
- 3
- 4
- 5













