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
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
Delivering safe C++My original design was for readonly and writeonly Stroustrup - C++ safety -CppCon - October 2023 19RAII (1979 and later) • From my 1979 lab book: • A “new function” creates the run-time environment for Exceptions • Guaranteed error-handling – or termination • Proper interaction with resource management (RAII) • Containers • No need to fiddle with arrays (and pointers) • Enable range checking • Algorithms Not articulating those caused problems that still lingerBenefits come from using C++ well • Use • RAII • const • Containers • Resource management pointers • Algorithms • Range-for • span • … • Avoid0 码力 | 74 页 | 2.72 MB | 6 月前3
Back to Basics: Concurrencydestructor of lock_guard will take care of releasing the lock ● std::lock_guard is a good example of RAII ○ lock_guard takes ownership of the lock, and when we leave scope the mutex is released (and the destructor of lock_guard will take care of releasing the lock ● std::lock_guard is a good example of RAII ○ lock_guard takes ownership of the lock, and when we leave scope the mutex is released (and the scoped_guard in that we can control locking and unlocking ○ Used in conditon_variable ○ Also follows RAII so we can use it safely. 98There exist several other primitives you can find here ● https://en.cppreference0 码力 | 141 页 | 6.02 MB | 6 月前3
现代C++ 教程:高速上手C++11/14/17/20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 第 5 章智能指针与内存管理 52 5.1 RAII 与引用计数 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 5.2 获得更好的性能。 std::tuple 虽然有效,但是标准库提供的功能有限,没办法满足运行期索引和迭代的需求,好在我 们还有其他的方法可以自行实现。 第 5 章智能指针与内存管理 5.1 RAII 与引用计数 了解 Objective-C/Swift 的程序员应该知道引用计数的概念。引用计数这种计数是为了防止内存泄 露而产生的。基本想法是对于动态分配的对象,进行引用计数,每当增加一次对同一个对象的引用,那 中,『记得』手动释放资源,总不是最佳实践。因为我们很有可能就忘记了去释放资源 而导致泄露。所以通常的做法是对于一个对象而言,我们在构造函数的时候申请空间,而在析构函数(在 离开作用域时调用)的时候释放空间,也就是我们常说的 RAII 资源获取即初始化技术。 凡事都有例外,我们总会有需要将对象在自由存储上分配的需求,在传统 C++ 里我们只好使用 new 和 delete 去『记得』对资源进行释放。而 C++11 引入了智0 码力 | 83 页 | 2.42 MB | 1 年前3
共 79 条
- 1
- 2
- 3
- 4
- 5
- 6
- 8













