Commit b4883edc authored by Jan Möbius's avatar Jan Möbius
Browse files

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
parent 52d0ebba
...@@ -63,6 +63,10 @@ ...@@ -63,6 +63,10 @@
#include <string> #include <string>
#include <algorithm> #include <algorithm>
#if __cplusplus > 199711L or __GXX_EXPERIMENTAL_CXX0X__
#include <mutex>
#endif
//== NAMESPACES =============================================================== //== NAMESPACES ===============================================================
...@@ -177,6 +181,11 @@ protected: ...@@ -177,6 +181,11 @@ protected:
// output what's in buffer_ // output what's in buffer_
virtual int sync() 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<std::mutex> lck (serializer_);
#endif
if (!buffer_.empty()) if (!buffer_.empty())
{ {
if (enabled_) multiplex(); if (enabled_) multiplex();
...@@ -196,7 +205,17 @@ protected: ...@@ -196,7 +205,17 @@ protected:
int_type overflow(int_type _c = multiplex_streambuf::traits_type::eof()) int_type overflow(int_type _c = multiplex_streambuf::traits_type::eof())
{ {
char c = traits_type::to_char_type(_c); 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<std::mutex> lck (serializer_);
buffer_.push_back(c);
}
#else
buffer_.push_back(c);
#endif
if (c == '\n') sync(); if (c == '\n') sync();
return 0; return 0;
} }
...@@ -236,6 +255,12 @@ private: ...@@ -236,6 +255,12 @@ private:
target_map target_map_; target_map target_map_;
std::string buffer_; std::string buffer_;
bool enabled_; 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 #undef STREAMBUF
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment