Search code examples
c++multithreadingvisual-c++locale

std::locale/std::facet Critical section


Out of curiosity. In the past I've seen performance degradation in function like boost::to_lower because of the CriticalSection employed in std::use_facet when the lazy facet is allocated. As far as I remember there was a bug with global lock on locale but according to Stephan Lavavej it was fixed in VS2013. And voila, I saw this lock on facet killing server performance yesterday so I guess I'm confusing two different issues.
But in the first place, why there is a CriticalSection around the lazy facet? Obviously it will ruin the performance. Why they didnt resolve to some kind of upgradable lock or atomic operations on pointers?


Solution

  • MSVC++'s std::locale is implemented in terms of the underlying C function setlocale. That touches global state, and must therefore be protected by a lock.

    Changing the locking semantics of a data structure is unfortunately an ABI breaking change, so not much we'll be able to do about it for a while.