From ff051629044833667e3ebc65655d39f3921bb49a Mon Sep 17 00:00:00 2001 From: Konstantin Morozov Date: Sun, 5 Feb 2023 19:25:09 +0300 Subject: [PATCH] change mutex to lock-free --- src/Common/MultiVersion.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/Common/MultiVersion.h b/src/Common/MultiVersion.h index 8a6eac0abad..4b138dc1a35 100644 --- a/src/Common/MultiVersion.h +++ b/src/Common/MultiVersion.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -39,19 +40,15 @@ public: /// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version. Version get() const { - /// NOTE: is it possible to lock-free replace of shared_ptr? - std::lock_guard lock(mutex); - return current_version; + return std::atomic_load(¤t_version); } /// Update an object with new version. void set(std::unique_ptr && value) { - std::lock_guard lock(mutex); - current_version = std::move(value); + std::atomic_store(¤t_version, Version{std::move(value)}); } private: - Version current_version TSA_GUARDED_BY(mutex); - mutable std::mutex mutex; + Version current_version; };