Did you try using thread_local locale objects? As you say, you only set
the global locale once in your program startup, you could then
initialize thread-specific copies from the global locale when you start
worker threads and then use the copies in all your algorithms. Although
that would not help if you're constructing a lot of streams - the stream
would still default-construct a locale on its construction.
That's the usual workaround. For example, with boost::iequal i created a wrapper function, and stored a thread local copy of the locale, which I pass to the boost::iequal -- problem solved until the next case. Another time it happened I had to create a thread local copy of a stringstream object, and always clear the stringstream.
I'm not really happy with that situation though. It would be nice if our developers could use the native std and boost libraries (like stringstream) without worrying about the possibility of a contention problem.
In 90% of the cases, the contention isn't a problem because the concurrent access is at a sane level. If we always create thread local instances, or write wrappers, that smacks of premature optimization. On the other hand it's often difficult to know in advance if the code you just wrote will wind up being used in such a way. Sometimes we make a release and things run smoothly until the customer changes a configuration variable, and suddenly contention becomes an issue.
On the other hand, at leas in my job experience, one time setting of the global locale has been the norm. I've only once had a system where changing the global locale object was part of the internationalization design, and even then we could have easily handled it otherwise.
I think the solution is to mandate that locales be per-thread, rather than merely allowing it.
--
--- You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-discussion+unsubscribe@isocpp.org.
To post to this group, send email to std-dis...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/std-discussion/.
I write C++ applications which run on servers with a lot of cores. We make use of std::locale's, but we don't change them during runtime (at least we only ever change global system locale once).
We suffer from the fact that accessing the global locale requires an atomic reference count implementation.