C++ io, thread safety, and setlocale

Benjamin Kosnik bkoz@redhat.com
Tue Apr 22 01:30:00 GMT 2003


>How can this be thread safe without some locking of some sort?

It's not. 

As Loren suggest in a later thread, perhaps the generic locale should
stop trying to be locale-correct and instead try to be thread safe. Or,
it should remove the last use of helper "C" functions that care about
the global locale (my preference). It's been apparent for some time that
you can't have both. There were some patches floating around that
excised the setlocale bits.

On mainline, and the gcc-3_3-branch, this problem (and a related speed
problem) is fixed for integer output because __convert_from_v is no
longer used. This is for all platforms. If you can, you might want to
use that, although the problem still exists for floating point output.
I'm hoping that it will soon be fixed for that as well. 

For linux, different code is used, which is thread safe. See
libstdc++-v3/config/locale/gnu/c_locale.h, which uses the uselocale
extensions in glibc. This is exactly like the "per thread locale" you
want to use on AIX, but which, to my understanding, does not exist.

I'm not quite sure what Solaris does, but maybe they do some
thread local storage tricks by default with pthreads.

-benjamin



More information about the Libstdc++ mailing list