]> git.sesse.net Git - stockfish/commitdiff
Introduce serialization of accesses to std::cout
authorMarco Costalba <mcostalba@gmail.com>
Wed, 29 Aug 2012 09:25:11 +0000 (11:25 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Wed, 29 Aug 2012 17:11:31 +0000 (19:11 +0200)
When many threds concurrently print you need to serialize
the access to std::cout to avoid output lines are intermixed
with the contents of each thread.

This is not strictly needed at the moment because
only main thread prints out, although some ad-hoc
test could trigger UCI::loop() printing while searching.

Anyhow we want to lift this pretty avoidable constrain
also as a prerequisite for future work.

This patch just introduces the support, next one will enable
the serialization.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/misc.cpp
src/misc.h

index 1a092c612fd2abc084acc27366d6644622c0d5d3..ababdbc1d7df8d4276e0de838a0ef34e5faf3071 100644 (file)
@@ -146,6 +146,23 @@ public:
 };
 
 
+/// Used to serialize access to std::cout to avoid multiple threads to write at
+/// the same time.
+
+std::ostream& operator<<(std::ostream& os, SyncCout sc) {
+
+  static Mutex m;
+
+  if (sc == io_lock)
+      m.lock();
+
+  if (sc == io_unlock)
+      m.unlock();
+
+  return os;
+}
+
+
 /// Trampoline helper to avoid moving Logger to misc.h
 void start_logger(bool b) { Logger::start(b); }
 
index 57e86553ffa0b1686ab7c06d8e36a6ba8d75da60..24d41d1a0e7a5e2ada7d6c5da492e10ff68d86b9 100644 (file)
@@ -65,4 +65,11 @@ private:
   std::vector<Entry> e;
 };
 
+
+enum SyncCout { io_lock, io_unlock };
+std::ostream& operator<<(std::ostream&, SyncCout);
+
+#define sync_cout std::cout << io_lock
+#define sync_endl std::endl << io_unlock
+
 #endif // !defined(MISC_H_INCLUDED)