Boost logo

Boost :

Subject: Re: [boost] Boost sprint: #3407 boost::call_once not re-entrant (at least in win32)
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2009-11-23 01:02:12

On Sun, Nov 22, 2009 at 11:49 AM, Vicente Botet Escriba
<vicente.botet_at_[hidden]> wrote:
> Hi,
> I have changed the ticket from threads to thread
> As this works as expected, Anthony could just add on the documentation why
> reentrant call_once functions are not a good idea. The explanation of Steven
> could be a good starting point:
> "I'll also point out that there is no good behavior for a recursive call to
> call_once. If it calls the function again, then f will be called twice. If
> it does not, then whatever initialization that call_once is doing may be
> incomplete. In either case, you have to know whether a call can be recursive
> and carefully design for it. If it happens accidentally, you're probably
> toast regardless of how it behaves. In the face of recursive calls it is
> impossible to maintain the post-condition that the function has run exactly
> once."
> After this modification IMO the ticket can be closed.
> Best,
> Vicente

First of all, I agree that a recursive call_once makes little sense.
However, I was in the process of writing a (better?) version of
call_once that avoided creating the system wide mutex (on Windows),
using a process-local Event instead (and even that is not created
unless there is contention - ie the second thread in creates the
While tackling this, I also decided to tackle ways of handling
exceptions (should 'f' be recalled if the first attempt threw an
exception?), as well as optionally allowing recursion.

The code is currently Windows-only, although it could be ported most anywhere.
I'd be interested to know:

- if the exception handling and/or recursion handling are worthwhile options
and more importantly
- if the avoidance of creating a named mutex is worthwhile (I think so)

Code attached. Not 100% tested yet (or 'boostified'), but I'm pretty
sure the algorithm is sound.


Boost list run by bdawes at, gregod at, cpdaniel at, john at