Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Programming Threads > One-Time Initia...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 12 Topic 4051 of 4146
Post > Topic >>

One-Time Initialization Convenient Template Class

by Brian Cole <coleb2@[EMAIL PROTECTED] > Sep 24, 2008 at 04:16 PM

I'm trying to write a convenient wrapper for doing one-time
initialization of function statics. For example:

void Foo()
{
  static Once<Bar> bar;
  // use bar in some known thread-safe fa****on
}

On GCC and in the new C++0x standard the Once class wouldn't actually
have to do anything since thread-safe static initialization is
sup****ted there. Unfortunately I have to sup****t a much wider range of
compilers: GCC, MSVC, XlC, Intel, suncc; and platforms: x86(_64), PPC,
IA64, Sparc. Therefore, I would like to avoid having to implement an
atomic class, which most other one-time initialization solutions seem
to rely on:
http://groups.google.com/group/comp.programming.threads/browse_thread/thread/a652518efef1c6bb/9d3e968f86f67cd7#c62122e48c9699aa
http://lists.boost.org/threads-devel/2006/10/0149.php
(boost::call_once uses atomics)

Fortunately, I am always guaranteed either the Windows threads or
pthreads. So I tried my best to slug out an implementation under that
constraint using the a dr. dobbs journal article as a guide:
http://www.ddj.com/cpp/199203083?pgno=3

I came up with the following:
template<class T>
class Once
{
  T *_obj;

#if defined(_WIN32) || defined(_WIN64)
  volatile LONG _mutex;
  T *GetValue()
  {
    if (_obj)
      return _obj;

    while (InterlockedExchange(&_mutex, 1) != 0)
      Sleep(1);

    if (!_obj)
      _obj = new T;

    InterlockedExchange(&_mutex, 0);

    return _obj;
  }

#else  /* _WIN32 || _WIN64 */
  pthread_mutex_t _mutex;
  T *GetValue()
  {
    if (_obj)
      return _obj;

    pthread_mutex_lock(&_mutex);
    if (!_obj)
      _obj = new T;
    pthread_mutex_unlock(&_mutex);

    return _obj;
  }
#endif /* _WIN32 || _WIN64 */

public:
#if defined(_WIN32) || defined(_WIN64)
  Once() : _obj(0), _mutex(0) {}
#else  /* _WIN32 || _WIN64 */
  Once() : _obj(0), _mutex(PTHREAD_MUTEX_INITIALIZER) {}
#endif /* _WIN32 || _WIN64 */

  inline T& operator *() const { return *GetValue(); }
  inline T* operator ->() const { return GetValue(); }
  inline operator T* () const { return operator ->(); }
  inline operator T& () const { return operator *(); }
};

It delays the initialization until the first access similar to the
solution in the boost mailing list thread. However, I can still see a
race condition that I know of no way of getting around. It looks like
the pointer (_obj) has to be initialized to zero somehow, however,
that initialization will race with the initialization when the actual
object is created. It appears to be the chicken or the egg, you need
to initialize that pointer somehow. The only thing that could save me
is if the compiler will statically initialize the memory of the
function static so there is no constructor initialization of _obj to
race with.

Is there any way to do what I ask without creating an atomic
implementation? Using only pthreads and Windows threads?

Thanks,
Brian
 




 12 Posts in Topic:
One-Time Initialization Convenient Template Class
Brian Cole <coleb2@[EM  2008-09-24 16:16:07 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-24 20:40:34 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-24 20:43:29 
Re: One-Time Initialization Convenient Template Class
Anthony Williams <anth  2008-09-25 08:41:51 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-25 02:32:08 
Re: One-Time Initialization Convenient Template Class
Anthony Williams <anth  2008-09-25 10:45:44 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-25 03:12:16 
Re: One-Time Initialization Convenient Template Class
Anthony Williams <anth  2008-09-25 11:39:56 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-25 04:03:11 
Re: One-Time Initialization Convenient Template Class
"Chris M. Thomasson&  2008-09-25 04:07:07 
Re: One-Time Initialization Convenient Template Class
Brian Cole <coleb2@[EM  2008-09-25 10:31:57 
Re: One-Time Initialization Convenient Template Class
Brian Cole <coleb2@[EM  2008-09-25 10:40:06 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Sat Nov 22 8:38:04 CST 2008.