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 libstdc++/12658] Thread safety problems in locale::global() and locale::locale()


------- 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


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