From a9f2eeb3bef1b1375f6c4b761617bd4853e0ed20 Mon Sep 17 00:00:00 2001 From: LoRd_MuldeR Date: Sun, 15 Apr 2018 15:12:13 +0200 Subject: [PATCH] Some improvements to Lazy class. --- include/MUtils/Lazy.h | 26 ++++++++++---------------- 1 file changed, 10 insertions(+), 16 deletions(-) diff --git a/include/MUtils/Lazy.h b/include/MUtils/Lazy.h index 904c1ec..a0031c4 100644 --- a/include/MUtils/Lazy.h +++ b/include/MUtils/Lazy.h @@ -53,26 +53,20 @@ namespace MUtils T& operator*(void) { T *value; - do + while (!(value = m_value.fetchAndAddOrdered(0))) { - if (!(value = m_value.fetchAndAddOrdered(0))) + if (!(value = m_initializer())) { - if (value = m_initializer()) - { - if (!m_value.testAndSetOrdered(NULL, value)) - { - delete value; - value = NULL; - } - } - else - { - MUTILS_THROW("Initializer returned NULL pointer!"); - } + MUTILS_THROW("Initializer returned NULL pointer!"); } + if (m_value.testAndSetOrdered(NULL, value)) + { + break; /*success*/ + } + delete value; + value = NULL; } - while (!value); - return *m_value; + return *value; } ~Lazy(void)