c++ - Calling `f()` once regardless of exceptions -
my understanding may incorrect but, reading documentation call_once, appears if multiple threads calling simultaneously same once_flag , first thread throws exception, 1 of other threads have callable called (and forth until 1 callable returns without throwing).
my question is, if have multiple thread same callable , want callable called once regardless of exception , want know exception, have no choice this:
void call_really_just_once() { std::exception_ptr e; std::call_once(some_once_flag_, [&] { try { may_throw(); } catch(...) { e = std::current_exception(); } }); if(e) { std::rethrow_exception(e); } }
Comments
Post a Comment