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
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
Modern C++ Tutorial: C++11/14/17/20 On the Fly. . . . . . . . . . . . . . . . . . . 57 Chapter 05 Smart Pointers and Memory Management 57 5.1 RAII and Reference Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 5 other methods that we can implement on our own. Chapter 05 Smart Pointers and Memory Management 5.1 RAII and Reference Counting Programmers who understand Objective-C/Swift/JavaScript should know the concept and free space when the destructor (called when leaving the scope). That is, we often say that the RAII resource acquisition is the initialization technology. There are exceptions to everything, we always0 码力 | 92 页 | 1.79 MB | 1 年前3
C++ Memory Model: from C++11 to C++23com/in/alexdathskovsky Synchronization Tools ● std::mutex, std::conditional_variable ● std::lock_guard – RAII helper for lockingAlex Dathskovsky | alex.dathskovsky@speedata.io | www.linkedin.com/in/alexdathskovsky unique and can be deferred. ● std::scoped_lock – takes ownership of multiple locks at once with RAII with dead lock avoidance algorithmAlex Dathskovsky | alex.dathskovsky@speedata.io | www.linkedin out. ● std::shared_timed_mutex – combination of shared and timed mutexes. ● std::shared_lock – RAII wrapper for timed and shared mutexesAlex Dathskovsky | alex.dathskovsky@speedata.io | www.linkedin0 码力 | 112 页 | 5.17 MB | 6 月前3
共 68 条
- 1
- 2
- 3
- 4
- 5
- 6
- 7













