From 47678b827e9b20a23352e381b16312f1c29831de Mon Sep 17 00:00:00 2001 From: Remi Gacogne Date: Tue, 14 Feb 2017 18:46:38 +0100 Subject: [PATCH] StateHolder: Allocate (and copy if needed) before taking the lock --- pdns/sholder.hh | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/pdns/sholder.hh b/pdns/sholder.hh index 457f33785..5a57ddc59 100644 --- a/pdns/sholder.hh +++ b/pdns/sholder.hh @@ -93,9 +93,12 @@ public: void setState(T state) //!< Safely & slowly change the global state { - std::lock_guard l(d_lock); - d_state = std::make_shared(T(state)); - d_generation++; + std::shared_ptr newState = std::make_shared(state); + { + std::lock_guard l(d_lock); + d_state = newState; + d_generation++; + } } T getCopy() const //!< Safely & slowly get a copy of the global state @@ -110,7 +113,7 @@ public: std::lock_guard l(d_lock); auto state=*d_state; // and yes, these three steps are necessary, can't ever modify state in place, even when locked! act(state); - d_state = std::make_shared(T(state)); + d_state = std::make_shared(state); ++d_generation; } -- 2.40.0