Comparing exception behavior of magic statics, std:call_once, and std:async
2 days ago
- Magic statics and std::call_once reattempt initialization if an exception occurs, leaving the state uninitialized.
- std::async saves the exception and rethrows it when the future is accessed, without reattempting.
- The choice between std::call_once and std::async depends on whether you want to retry or remember a failure.
- For trivial variables, using std::atomic with a sentinel value can be more performant than magic statics.
- The std::atomic sentinel approach requires the calculation to be safe to repeat and may not suit registration tokens.