Thomas Martin
Thomas Martin

Reputation: 112

Behaviour of call_once while exceptions

If I call a function with call_once and if it throws exception before completing execution, what will be the value of once_flag?

Upvotes: 2

Views: 102

Answers (1)

Praetorian
Praetorian

Reputation: 109189

once_flag state is not modified so the next call to call_once with the same once_flag will invoke the callable.

From [thread.once.callonce]/2 (emphasis added)

Effects: An execution of call_­once that does not call its func is a passive execution. An execution of call_­once that calls its func is an active execution. An active execution shall call INVOKE(​std​::​forward<Callable>(func), std​::​forward<Args>(args)...). If such a call to func throws an exception the execution is exceptional, otherwise it is returning. An exceptional execution shall propagate the exception to the caller of call_­once. Among all executions of call_­once for any given once_­flag: at most one shall be a returning execution; if there is a returning execution, it shall be the last active execution; and there are passive executions only if there is a returning execution.

Upvotes: 4

Related Questions