This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug c++/13684] local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads
- From: "gianni at mariani dot ws" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 15 Jul 2004 05:11:19 -0000
- Subject: [Bug c++/13684] local static object variable constructed once but ctors and dtors called multiple times on same memory when called in multiple threads
- References: <20040114172507.13684.evijaykumar@yahoo.com>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- 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