From 2252bfa9ac30d33351cd567dcbd4bb46a35650c8 Mon Sep 17 00:00:00 2001
From: Yichao Yu <yyc1992@gmail.com>
Date: Sun, 22 Jun 2014 06:21:15 +0800
Subject: [PATCH] better debug msg for multi-thread

---
 src/c_wrapper/debug.cpp | 2 ++
 src/c_wrapper/debug.h   | 3 +++
 src/c_wrapper/error.h   | 2 ++
 3 files changed, 7 insertions(+)

diff --git a/src/c_wrapper/debug.cpp b/src/c_wrapper/debug.cpp
index fbc69c90..c5806891 100644
--- a/src/c_wrapper/debug.cpp
+++ b/src/c_wrapper/debug.cpp
@@ -4,6 +4,8 @@
 
 namespace pyopencl {
 
+std::mutex dbg_lock;
+
 void
 dbg_print_str(std::ostream &stm, const char *str, size_t len)
 {
diff --git a/src/c_wrapper/debug.h b/src/c_wrapper/debug.h
index 1b8c2ca7..34491481 100644
--- a/src/c_wrapper/debug.h
+++ b/src/c_wrapper/debug.h
@@ -1,6 +1,7 @@
 #include "wrap_cl.h"
 #include "function.h"
 #include <string.h>
+#include <mutex>
 
 #ifndef __PYOPENCL_DEBUG_H
 #define __PYOPENCL_DEBUG_H
@@ -16,6 +17,8 @@ extern bool debug_enabled;
 
 #define DEBUG_ON (PYOPENCL_EXPECT(debug_enabled, DEFAULT_DEBUG))
 
+extern std::mutex dbg_lock;
+
 void dbg_print_str(std::ostream&, const char*, size_t);
 static PYOPENCL_INLINE void
 dbg_print_str(std::ostream &stm, const char *str)
diff --git a/src/c_wrapper/error.h b/src/c_wrapper/error.h
index dfe4654e..9efb7de3 100644
--- a/src/c_wrapper/error.h
+++ b/src/c_wrapper/error.h
@@ -26,6 +26,7 @@ public:
         : std::runtime_error(msg), m_routine(rout), m_code(c)
     {
         if (DEBUG_ON) {
+            std::lock_guard<std::mutex> lock(dbg_lock);
             std::cerr << rout << ";" << msg<< ";" << c << std::endl;
         }
     }
@@ -186,6 +187,7 @@ public:
     {
         auto res = this->template call<__CLArgGetter>(func);
         if (DEBUG_ON) {
+            std::lock_guard<std::mutex> lock(dbg_lock);
             _print_trace(res, name);
         }
         return res;
-- 
GitLab