Created
March 4, 2022 17:26
-
-
Save prwhite/3c2dddc6159bf1d1b4571ac930988603 to your computer and use it in GitHub Desktop.
Cheesy thread-synchronized std::cout output.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////////////// | |
// Class to substitute for cout to get synchronization of output. | |
// Usage: syncout << "foo " << 0xff << syncendl; | |
using namespace std; | |
class syncostringstream : public ostringstream {}; | |
#define syncout syncostringstream() | |
ostream &syncendl(ostream &ostr) | |
{ | |
if(auto *sostr = dynamic_cast< syncostringstream* >( &ostr )) | |
{ | |
static mutex m; | |
unique_lock< mutex > lock(m); | |
ostr << endl; | |
cout << sostr->str(); | |
} | |
else | |
cerr << "ostream type not an syncostringstream, cannot synchronize output" << endl; | |
return ostr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment