pool: add call_once to initialize_pool() mem. fun
All checks were successful
continuous-integration/drone/push Build is passing

* also protect set_id() member function with a std::lock_guard, which
  holds mtx for the time of initialization
This commit is contained in:
surtur 2022-01-22 21:14:01 +01:00
parent 14462e6b16
commit e452a63b4a
Signed by: wanderer
GPG Key ID: 19CE1EC1D9E0486D
2 changed files with 4 additions and 1 deletions

View File

@ -15,6 +15,7 @@ namespace fortuna {
namespace accumulator {
auto Pool::set_id(const unsigned int& id) noexcept -> void {
std::lock_guard<std::mutex> lg(mtx);
this->pool_id = id;
}
@ -23,7 +24,8 @@ auto Pool::get_id() const noexcept -> unsigned int {
}
auto Pool::initialize_pool(const unsigned int& id) -> void {
set_id(id);
// ref: https://stackoverflow.com/a/23204682
std::call_once(init, [this, id]{ this->set_id(id); } );
}
auto Pool::add_entropy(const unsigned int& source,

1
pool.h
View File

@ -43,6 +43,7 @@ private:
uint64_t size{0};
std::mutex mtx; // mutex for write locks
mutable std::recursive_mutex ro_mtx_s; // mtx for readers of s
std::once_flag init;
}; // class Pool