From cfdba14e35553469fb71b82cc433055660940474 Mon Sep 17 00:00:00 2001 From: Jan Haller Date: Sun, 6 Dec 2020 18:03:57 +0100 Subject: [PATCH] Keep thread-local instances of sf::err() backing implementation (WIP) --- src/SFML/System/Err.cpp | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/src/SFML/System/Err.cpp b/src/SFML/System/Err.cpp index 377da23e0..ee1f34a0a 100644 --- a/src/SFML/System/Err.cpp +++ b/src/SFML/System/Err.cpp @@ -26,6 +26,8 @@ // Headers //////////////////////////////////////////////////////////// #include +#include +#include #include #include @@ -93,6 +95,23 @@ private: return 0; } }; + +// Groups a std::ostream with its std::streambuf +struct ThreadLocalErr : sf::NonCopyable +{ + DefaultErrStreamBuf buffer; + std::ostream stream; + + ThreadLocalErr() : + buffer(), + stream(&buffer) + { + } +}; + +// This per-thread variable holds the current instance of DefaultErrStreamBuf +sf::ThreadLocalPtr currentErr(NULL); + } namespace sf @@ -100,11 +119,12 @@ namespace sf //////////////////////////////////////////////////////////// std::ostream& err() { - static DefaultErrStreamBuf buffer; - static std::ostream stream(&buffer); + if (currentErr == NULL) + { + currentErr = new ThreadLocalErr(); + } - return stream; + return currentErr->stream; } - } // namespace sf