Coroutines are Qt: safer thread pools interactions
summary
A C++20 coroutine-based pattern replaces nested callbacks with a sequential function that bounces between main thread and thread-pool using C++20 coroutines and self-dispatching awaitables. It auto-checks lifetimes by wrapping UI pointers in a guarded UI pointers tuple that nulls them on worker switch, and protects backend access with an expiring backend pointer. Implemented in portable C++ (works with Qt), tests pass and expose deterministic crashes for race bugs; tradeoffs: heap allocation and swap overhead, and compiler/debugger support is still immature.
AI-written summary · about
chapters (39)
- 0:00Intro
- 1:01Outline
- 2:28Motivation - The Ping-Pong Pattern
- 5:40Challenges
- 8:12Running Example: Remarks
- 8:47Approach 1 - The Qt Way: Qobjects
- 13:27Approach 2 - Nested Lambdas
- 15:57Other Approaches
- 16:44Coroutines - High-Level Overview
- 17:30Coroutines - Supported Operations
- 20:38Coroutines - Pseudo-Code
- 22:59Coroutines in C++
- 24:49C++ Coroutines - Notable Objects
- 26:16C++ Coroutines - New Coroutine Operator
- 27:22C++ Coroutines - Practical Example
- 30:28C++ Coroutines - Awaitable Interface
- 32:07C++ Coroutines - co_await on Awaitables
- 33:33Design Principles
- 34:03Proposed Solution - 4 Incremental Steps
- 35:33Self-Dispatching Coroutines - Custom Await
- 36:15Self-Dispatching Coroutines - Running Exa
- 37:19Self-Dispatching Coroutines - Recap
- 37:56Automate Lifetime Checks - Problem State
- 38:44Automate Lifetime Checks - Example of Desir
- 39:21Automate Lifetime Checks - Guard clas
- 40:17Automate Lifetime Checks - Checked Await
- 41:03Automate Lifetime Checks - Running Exar
- 42:022. Automate Lifetime Checks - Recap
- 42:21Guarding Thread-Unsafe Pointers - Fronten
- 42:56Guarding Thread-Unsafe Pointers - Improved
- 43:31Guarding Thread-Unsafe Pointers - Swapp
- 44:07Guarding Thread-Unsafe Pointers - Rec
- 44:25Guarding Thread-Unsafe Pointers - Backen
- 44:46Guarding Thread-Unsafe Pointers - Running
- 45:34Guarding Thread-Unsafe Pointers - Expirit
- 45:55Guarding Thread-Unsafe Pointers - ToThrea
- 46:35Final Reworked Example
- 47:34Final Design - Remarks
- 50:19Hidden Costs
related talks
52mCoroutines: C++ vs Rust
47mHow to start using coroutines
57mCoroutine Intuition in C++
01h 05mCoroutines: C++ vs Rust
01h 14mHow to Use C++20 Coroutines for Networking
01h 16mC++ Coroutines From Scratch
01h 27mManage Asynchronous Control Flow With C++ Coroutines
01h 26mC++ Coroutines - Don’t Try This at Home
