Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2002-08-06 16:42:24


Pete Becker wrote:
[...]
> To paraphrase my recollection of Butenhof's discussion: back in the olden
> days, before POSIX had static initializers for mutexes, once functions were
> essential, so that you could guarantee that a mutex had been initialized
> before you used it. That was their primary reason for existing. Once static
> initialion of mutexes was added this problem went away, and with it, the
> primary need for once functions.
>
> Further, a once function won't ever be more efficient than user-written
> code that uses a mutex and a flag, and will often be less efficient. So as
> long as you can statically initialize a mutex, once functions are "merely"
> a convenience.

< from my e-mail archive, censored >

cc: <censored/snip>
Subject: Re: Std 1003.1-2001 Aardvarks: XBD ERN 10/11 (XSH ERN 44, partly)

David Butenhof (David.Butenhof_at_[hidden]) wrote:

<censored/snip>

> Ulrich's point (something I hadn't initially considered)

You did! Remember the e-mail from me and your reply w.r.t TSD-fix
for Classic-DCL some time ago? ;-)

> is that on some
> implementations it's possible to optimize pthread_once() so that only
> the first call (for a given pthread_once_t) in each thread pays the cost
> of memory synchronization. Generally, this is done by recording in TSD
> (or similar) state that the thread has done the initial check on
> initialization. Subsequent calls are essentially "free".

Yep. Just like TSD-fixed-DCL (but the "flag" should NOT
be set if once_routine "throws" [in the init-thread], I
mean C-THREADS-CANCEL, for example). Java's illustration:

  class Singleton {
    private static Singleton theInstance;

    private static final ThreadLocal perThreadInstance =
      new ThreadLocal() {
          public Object initialValue() { return createInstance(); }
        };

    public static Singleton getInstance() {
      return (Singleton)perThreadInstance.get();
    }

    private static synchronized Singleton createInstance() {
      if (theInstance == null)
        theInstance = new Singleton();
      return theInstance;
    }
  }

> However, a
> blanket requirement that pthread_once() provide memory synchronization
> outlaws such sensible optimizations for no gain.

Yup, but... well, exact protocols/semantics aren't really defined
anyway (and the routine is called only ONCE as well... so, "once"
synchronized [per-thread with init-thread only] there is just nothing
left to "worry"/be required in addition to that mem.sync w.r.t.
init-thread/once-completion ONLY), AFAICT. ;-)

<censored/snip>

regards,
alexander.


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