Hi all,
It looks like DeviceAPIManager::Global() is mis-implemented. For a singleton pattern, it may ought to be implemented as follows:
index d042cb406..772f18ebe 100644
--- a/src/runtime/c_runtime_api.cc
+++ b/src/runtime/c_runtime_api.cc
@@ -35,6 +35,7 @@
#include <cstdlib>
#include <sstream>
#include <string>
+#include <mutex>
#include "object_internal.h"
#include "runtime_base.h"
@@ -105,7 +106,12 @@ class DeviceAPIManager {
DeviceAPIManager() { std::fill(api_.begin(), api_.end(), nullptr); }
// Global static variable.
static DeviceAPIManager* Global() {
- static DeviceAPIManager* inst = new DeviceAPIManager();
+ static std::mutex sinst_mutex;
+ static DeviceAPIManager* inst = nullptr;
+
+ std::lock_guard<std::mutex> lock(sinst_mutex);
+ if (!inst)
+ inst = new DeviceAPIManager();
return inst;
}
// Get or initialize API.