[EMCpp]Item-39 Consider Void Futures for One-Shot Event Communication
· ☕ 3 min read
Using std::promise
s and futures is useful skill to create one-shot communication between a detecting task and reacting task.
Using std::promise
s and futures is useful skill to create one-shot communication between a detecting task and reacting task.
Future destructors normally just destroy the future’s data members, execept for the final future referring to a shared state for a non-deferred task launched via std::async
, which blocks until the task completes.
Join-on-destruction can lead to difficult-to-debug performance anomalies; while detach-on-destruction can lead to difficult-to-debug undefined behavior.
The flexibility that default policy for std::async
permits both async and sync task execution leads to uncertainty when accessing thread_locals
, implies that the task may never execute, and affects program logic for timeout-based wait
calls.
Thread-based programming calls for manual management of thread exhaustion, oversubscription, load balancing, and adaptation to new platforms, while task-based programming via std::async
with the default launch policy handles most of the issues for us.