From edb18987c495d4fc3b512d1f1114c56137050007 Mon Sep 17 00:00:00 2001 From: morningman Date: Wed, 2 Sep 2020 21:08:25 +0800 Subject: [PATCH] [Bug] Fix bug of core local value When creating core local value from CoreDataAllocator, A lock is needed to protect the modification of _blocks. --- be/src/util/core_local.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/be/src/util/core_local.cpp b/be/src/util/core_local.cpp index 9c9a8a4cd384e9..30cda25f078852 100644 --- a/be/src/util/core_local.cpp +++ b/be/src/util/core_local.cpp @@ -21,6 +21,7 @@ #include #include "common/logging.h" +#include "util/spinlock.h" namespace doris { @@ -48,8 +49,11 @@ class CoreDataAllocatorImpl : public CoreDataAllocator { virtual ~CoreDataAllocatorImpl(); void* get_or_create(size_t id) override { size_t block_id = id / ELEMENTS_PER_BLOCK; - if (block_id >= _blocks.size()) { - _blocks.resize(block_id + 1); + { + std::lock_guard l(_lock); + if (block_id >= _blocks.size()) { + _blocks.resize(block_id + 1); + } } CoreDataBlock* block = _blocks[block_id]; if (block == nullptr) { @@ -61,6 +65,7 @@ class CoreDataAllocatorImpl : public CoreDataAllocator { } private: static constexpr int ELEMENTS_PER_BLOCK = BLOCK_SIZE / ELEMENT_BYTES; + SpinLock _lock; // lock to protect the modification of _blocks std::vector _blocks; };