This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/13684] local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads


------- Additional Comments From gianni at mariani dot ws  2004-07-15 05:11 -------

> It's not quite a one-time thing, of course: every time you run over 
> a static variable, you have to check whether it has already been  
> initialized.

I suspect that this is done with the current gcc.

> If this check has to be guarded by a lock, it gets vastly 
> more expensive than just checking for zero-or-one. 

I don't understand why you think so.

Zack's suggestion (and this concept is also used in lots of other code) is this:

  if (guard) {
    if (__cxa_guard_acquire (&guard)) {
      // construct variable.
      __cxa_guard_release (&guard)
    }
  }

The first conditional is not thread safe, however, once the object is correctly
initialized the first conditional will be false and hence it will render code
that is practically the same speed as non thread-safe code (code size will be
larger, obviously!).


 

-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13684


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]