This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug libstdc++/12658] Thread safety problems in locale::global() and locale::locale()
- From: "peturr02 at ru dot is" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 28 Jul 2004 17:58:02 -0000
- Subject: [Bug libstdc++/12658] Thread safety problems in locale::global() and locale::locale()
- References: <20031017095219.12658.peturr02@ru.is>
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
------- Additional Comments From peturr02 at ru dot is 2004-07-28 17:58 -------
Before anyone says "don't do that then" ...
This sort of usage can be perfectly safe (and sometimes even neccessary):
Thread 1 needs to do some formatting with some crufty C code, and thus needs
to set the global locale:
void thread1()
{
// Set global locale to "C"
locale tmp = locale::global(locale("C"));
// Do some C style formatting
fprintf(f, ...);
// Reset global locale
locale::global(tmp);
}
Thread 2 also needs to format, but uses IOstreams. Because thread 1 may be
playing games with the global locale, take care always to call imbue:
void thread2()
{
// Thread specific locale
locale myloc("");
fstream f;
// Take care not to use global locale, can't be trusted.
f.imbue(myloc);
// Do some C++ style formatting
f << ...
}
The two threads don't share any user-visible data, so this sort of usage
should be thread-safe. The only problem is that fstream::fstream() will
call locale::locale(), which touches _S_global, a variable that the user
should need to know about.
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=12658