From: Remi Gacogne Date: Tue, 14 Feb 2017 17:46:38 +0000 (+0100) Subject: StateHolder: Allocate (and copy if needed) before taking the lock X-Git-Tag: rec-4.1.0-alpha1~267^2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=47678b827e9b20a23352e381b16312f1c29831de;p=pdns StateHolder: Allocate (and copy if needed) before taking the lock --- 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; }