[boost] Re: Enhanced call_once()
by Alexander Terekhov other posts by this author
Aug 6 2002 9:42PM messages near this date
Re: [boost] Enhanced call_once()
|
Re: [boost] Enhanced call_once()
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@HP.com) 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.
_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Thread:
Andrew J Bromage
Peter Dimov
Andrew J Bromage
William E. Kempf
Andrew J Bromage
William E. Kempf
Alexander Terekhov
Pete Becker
Andrew J Bromage
Peter Dimov
Andrew J Bromage
William E. Kempf
Andrew J Bromage
William E. Kempf
Rene Rivera
William E. Kempf
Andrew J Bromage
Jason Stewart
Andrew J Bromage
Andrew J Bromage
Rob Stewart
William E. Kempf
William E. Kempf
Alexander Terekhov
Peter Dimov
Peter Dimov
Pete Becker
William E. Kempf
William E. Kempf
Pete Becker
|