Boost logo

Boost :

From: William Kempf (williamkempf_at_[hidden])
Date: 2001-09-07 11:13:21


From: flameframe_at_[hidden]
>> > > > - implementation of call_once will fail when called function
>> > throws
>> > >
>> > > I didn't think about this one. I'm not sure if we should try
>> > > and "fix" call_once or document that such functions must not
>>throw.
>> >
>> > Fix is easy - instead of
>> > if (!flag)
>> > {
>> > func();
>> > flag = true;
>> > }
>> > should be
>> > if (!flag)
>> > {
>> > flag = true;
>> > func();
>> > }
>>
>>Not so easy. This creates a race condition.
>
>Could you elaborate ? This section is guarded by mutex.

When a thread calls call_once it must be gauranteed that the associated
method has been called before returning (in addition to insuring only one
thread calls it). Setting the flag to true before the function is called
creates a race condition in which a thread can return from the call to
call_once before the associated method has been called.

>>Plus claiming that
>>an "init method" has been run, when that method failed, can lead to
>>hard to diagnose bugs.
>>I'm leaning towards the no throw
>>documentation for this reason.
>
>And what if it throws ? Undefined behaviour ?

Yes.

>Let pretend that someone makes an once guard for function that should
>throw. To match the documentation code will be just wrapped with catch
>(...){} and became much more error-prone than throwing version.

The wrapper should in some way indicate (to ALL threads, not just the one
that managed to call the routine) that there was a failure, such as through
global data. This is actually less error prone, because all threads are
aware of the failure in a thread safe manner. You can't do this if the
routine throws an exception.

>Throwing exception, especially from complex function, is a most
>useful way to catch the problem. Being restricted to 'no throw'
>programmers will something like static error codes ( as errno ).

Exceptions can't be thrown across multiple threads, so the "static error
codes" approach is the only valid approach to take here.

Bill Kempf

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp


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