Boost logo

Boost :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2005-09-05 12:10:53


From: "Anthony Williams"

> "Peter Dimov" <pdimov_at_[hidden]> writes:
>
>> Anthony Williams wrote:
>>> Also, the semaphore version runs the function once, even if it
>>> throws an exception. The mutex version runs the function repeatedly
>>> if it throws an exception. The docs say that the function isn't
>>> allowed to throw, so this is a moot point.
>>
>> I like the signature of call_once:
>>
>> template<typename Function>
>> void call_once(Function f, once_flag& flag)
>>
>> but you need to fix the above behavior. The function is considered "run"
>> only when it returns without an exception. So your semaphore version runs
>> it
>> zero times when an exception is thrown.
>
> void f()
> {
> throw "something";
> }
>
> int main()
> {
> try
> {
> f();
> }
> catch(...){}
> }
>
> How many times has f() been *called*? One, or none? I find your concept of
> a
> function only having run if it exits without an exception odd.

It has been called once, but it didn't run to completion, and hence, its
postconditions haven't been satisfied. Therefore, if a subsequent call_once
returns successfully, as if nothing happened, the code will proceed under
the assumption that the postconditions are met, and will break in various
ways.

Local statics retry construction on exception for the same reason.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk