Here’s my number; call me, maybe. Callbacks in a multithreaded world
summary
Describes concrete fixes for callback chaos in task-based C++: use executors/thread pools, never invoke user code while holding a mutex, and queue callbacks to a single worker to preserve order and allow safe unregistering. Prefer capture by value not reference to avoid dangling references, use shared_ptr ownership (weak_ptr creates subtle races where destruction can run on a worker thread), and signal shutdown with stop_source/stop_token (P0660) or a task counter wrapper that waits until active tasks reach zero before destroying resources.
AI-written summary · about
chapters (24)
- 0:00Intro
- 0:15Overview
- 0:57Tossbased programming
- 3:46Executors
- 6:32Callbacks
- 7:53Race Conditions
- 8:50Base Conditions
- 10:11Multithreaded code
- 11:48First solution
- 12:34Downsides
- 15:37Queue
- 22:04Lifetime issues
- 22:26A simple example
- 24:04Valuebased programming
- 25:28Reference
- 26:37Watch for problems
- 29:50Data object
- 31:17Hanging tasks
- 32:17Weak pointer
- 35:32Stop sauce
- 36:32Stop request
- 38:11Stop callback
- 48:08Guidelines
- 51:02Alternatives
related talks
01h 08mBetter Code: Concurrency
01h 03mGet Off My Thread: Techniques for Moving Work to Background Threads
59mAn Introduction to Multithreading in C++20
47m“Multithreading is the answer. What is the question? (part 1 of 2)”
50m“Multithreading is the answer. What is the question? (part 2 of 2)”
01h 04mConcurrency in C++20 and Beyond
01h 28mAn Introduction to Multithreading in C++20
47mConcurrent Programming in C++
