From 5673c6f14a1712055ceddbf4614a1c8274354acb Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 15 Apr 2018 02:20:09 +0200 Subject: [PATCH] Some improvements to Lazy class. --- include/MUtils/Lazy.h | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/include/MUtils/Lazy.h b/include/MUtils/Lazy.h index e5fb12d..904c1ec 100644 --- a/include/MUtils/Lazy.h +++ b/include/MUtils/Lazy.h @@ -52,20 +52,26 @@ namespace MUtils T& operator*(void) { - while (!m_value) + T *value; + do { - if (T *const value = m_initializer()) + if (!(value = m_value.fetchAndAddOrdered(0))) { - if (!m_value.testAndSetOrdered(NULL, value)) + if (value = m_initializer()) { - delete value; /*too late*/ + if (!m_value.testAndSetOrdered(NULL, value)) + { + delete value; + value = NULL; + } + } + else + { + MUTILS_THROW("Initializer returned NULL pointer!"); } } - else - { - MUTILS_THROW("Initializer returned NULL!"); - } } + while (!value); return *m_value; }