From 9a08ad4252f07b6e673fd06158948e028750ca44 Mon Sep 17 00:00:00 2001 From: Shengyun Yao Date: Wed, 9 Oct 2019 16:21:08 +0800 Subject: [PATCH] Fixed: add mutex for cpu_info init() to prevent random core dump caused by init() in multi threads. --- be/src/util/cpu_info.cpp | 6 +++++- be/src/util/cpu_info.h | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/be/src/util/cpu_info.cpp b/be/src/util/cpu_info.cpp index 7bdb120e3d9ec6..05cb6492de5342 100755 --- a/be/src/util/cpu_info.cpp +++ b/be/src/util/cpu_info.cpp @@ -81,6 +81,7 @@ int CpuInfo::max_num_numa_nodes_; unique_ptr CpuInfo::core_to_numa_node_; vector> CpuInfo::numa_node_to_cores_; vector CpuInfo::numa_node_core_idx_; +pthread_mutex_t CpuInfo::init_mutex_; static struct { string name; @@ -111,7 +112,9 @@ int64_t ParseCPUFlags(const string& values) { } void CpuInfo::init() { - if (initialized_) return; + pthread_mutex_lock(&init_mutex_); + if (initialized_) return; + string line; string name; string value; @@ -173,6 +176,7 @@ void CpuInfo::init() { _init_numa(); initialized_ = true; + pthread_mutex_unlock(&init_mutex_); } void CpuInfo::_init_numa() { diff --git a/be/src/util/cpu_info.h b/be/src/util/cpu_info.h index 071e22fc4cb340..d6a4e849acd836 100755 --- a/be/src/util/cpu_info.h +++ b/be/src/util/cpu_info.h @@ -219,6 +219,9 @@ class CpuInfo { /// Array with 'max_num_cores_' entries, each of which is the index of that core in its /// NUMA node. static std::vector numa_node_core_idx_; + + // used for init in multi thread + static pthread_mutex_t init_mutex_; }; } #endif