_LOCK_LOCALE causes unavoidable multi-threaded contention on most basic_ostream functionality
Various operations within locale code use _LOCK_LOCALE to take a global lock. Because most basic_ostream methods hit this locale code (e.g. via use_facet), this means that most basic_ostream operations also end up taking a global lock.
The net effect of this as we discovered in our heavily multithreaded code base is that basic_ostream is completely non performant. It doesn't matter if you carefully manage your output streams (e.g. one per thread), you end up hitting contention on this global lock across all threads.
Is there a solution to this other than avoiding basic_ostream in multi threaded code?
Thanks for reporting this issue - we're not aware of any solutions other than avoidance :crying_cat_face: I've marked this as a vNext performance issue, since I don't believe we can change how the locale lock machinery works without breaking ABI.
Thanks Stephan. Yeah I was looking into it further myself, tbh the entire _BEGIN_LOCK/_END_LOCK() system seems pretty suspect. While it makes sense on global/static level calls, I don't think anyone would be expecting completely isolated instances of STL types to be hitting a global lock.
Various operations within locale code use _LOCK_LOCALE to take a global lock. Because most basic_ostream methods hit this locale code (e.g. via use_facet), this means that most basic_ostream operations also end up taking a global lock.
We got the same issue. See https://github.com/microsoft/STL/issues/1652#issue-809705500