From 1654bfee4d0cd4867249729de4a609aebcc02d9a Mon Sep 17 00:00:00 2001 From: Eugene Kosov Date: Thu, 11 Mar 2021 10:56:54 +0300 Subject: remove initialization --- include/distributable_counter.h | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/include/distributable_counter.h b/include/distributable_counter.h index eb309f6819c..144372f45b0 100644 --- a/include/distributable_counter.h +++ b/include/distributable_counter.h @@ -88,6 +88,9 @@ template class counter_broker_array; // collection of counters. In a nutshell it's just an array of std::atomics. // Counters can be incremented directly but when contention becomes a problem, // this counter can be distributed via counter_broker_array. + +// WARNING: use as a global variable only! Otherwise, deal with uninitialized +// memory template class distributable_counter_array { distributable_counter_array(const distributable_counter_array &)= delete; @@ -102,8 +105,8 @@ public: distributable_counter_array() noexcept { - for (auto &counter : counters_) - counter.store(0, std::memory_order_relaxed); + // No initialization of atomics here because we're relying on zero + // initialization for globals } __attribute__((warn_unused_result)) detail::strong_bumper @@ -226,7 +229,7 @@ public: operator[](size_t idx) { assert(idx < Size); - return local()[idx]; + return broker_[idx]; } __attribute__((warn_unused_result)) Integral load(size_t idx) @@ -239,15 +242,8 @@ public: } private: - counter_broker_array &local() - { - // Meyers' singleton ensures that the broker will be initialized on the - // first access and thus will not slow down thread creation. - thread_local counter_broker_array broker(global_); - return broker; - } - distributable_counter_array global_; + counter_broker_array broker_{global_}; }; #endif -- cgit v1.2.1