From b4883edc3e701a88e64a05fc3f2b06f8dcf3a69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20M=C3=B6bius?= Date: Mon, 26 Jan 2015 10:00:38 +0000 Subject: [PATCH] Fixed crash when writing with multiple threads to mostream. closes #2293 Note: This fix requires C++11 support git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@1212 fdac6126-5c0c-442c-9429-916003d36597 --- src/OpenMesh/Core/System/mostream.hh | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/OpenMesh/Core/System/mostream.hh b/src/OpenMesh/Core/System/mostream.hh index edc1b123..b6c72404 100644 --- a/src/OpenMesh/Core/System/mostream.hh +++ b/src/OpenMesh/Core/System/mostream.hh @@ -63,6 +63,10 @@ #include #include +#if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__ + #include +#endif + //== NAMESPACES =============================================================== @@ -177,6 +181,11 @@ protected: // output what's in buffer_ virtual int sync() { + // If working on multiple threads, we need to serialize the output correctly (requires c++11 headers) + #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__ + std::lock_guard lck (serializer_); + #endif + if (!buffer_.empty()) { if (enabled_) multiplex(); @@ -196,7 +205,17 @@ protected: int_type overflow(int_type _c = multiplex_streambuf::traits_type::eof()) { char c = traits_type::to_char_type(_c); - buffer_.push_back(c); + + // If working on multiple threads, we need to serialize the output correctly (requires c++11 headers) + #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__ + { + std::lock_guard lck (serializer_); + buffer_.push_back(c); + } + #else + buffer_.push_back(c); + #endif + if (c == '\n') sync(); return 0; } @@ -236,6 +255,12 @@ private: target_map target_map_; std::string buffer_; bool enabled_; + + // If working on multiple threads, we need to serialize the output correctly (requires c++11 headers) + #if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__ + std::mutex serializer_; + #endif + }; #undef STREAMBUF