Boost logo

Boost :

From: Alexander Terekhov (terekhov_at_[hidden])
Date: 2003-12-17 13:46:43


Michael Glassford wrote:
[...]
> >> void call_once(void (*func)(), once_flag& flag)
> >> {
> >> pthread_once(&once, &key_init);
> >> pthread_setspecific(key, &func);
> >> pthread_once(&flag, do_once);
> >> }
[...]
> So: does it work?

Yep (overhead and lack of error checking/reporting aside for a moment).
Synchronized local statics (but not by default) and "once_call<>" (for
the dynamic stuff) would work much better, of course.

typedef aligned_storage< once_call< void > > pthread_once_t;

extern "C" int pthread_once(pthread_once_t * once_control,
        void (* init_routine)()) {
  once_control->object()(init_routine);
  return 0;
}

extern "C++" int pthread_once(pthread_once_t * once_control,
        void (* init_routine)()) {
  once_control->object()(init_routine);
  return 0;
}

Or something like that. See

http://groups.google.com/groups?selm=3ECB8F71.689E551C%40web.de

regards,
alexander.


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