STL icon indicating copy to clipboard operation
STL copied to clipboard

_LOCK_LOCALE causes unavoidable multi-threaded contention on most basic_ostream functionality

Open rwyborn opened this issue 5 years ago • 3 comments

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?

rwyborn avatar Apr 13 '21 03:04 rwyborn

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.

StephanTLavavej avatar Apr 14 '21 21:04 StephanTLavavej

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.

rwyborn avatar Apr 15 '21 01:04 rwyborn

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

AndrewJD79 avatar Jun 24 '21 14:06 AndrewJD79