From fbf4e63098e6fbe3a0f0f4e62882faf76aed1de9 Mon Sep 17 00:00:00 2001 From: Zhou Wang Date: Tue, 6 Sep 2016 17:49:19 +0200 Subject: [PATCH] Use atomic variable to make creation of unique name thread-safe. remove blankline --- Source/Common/Include/Sequences.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/Source/Common/Include/Sequences.h b/Source/Common/Include/Sequences.h index 1e0e1b44e..902751d7e 100644 --- a/Source/Common/Include/Sequences.h +++ b/Source/Common/Include/Sequences.h @@ -7,10 +7,11 @@ #pragma once -#include "Basics.h" -#include "Matrix.h" #include #include // for shared_ptr +#include +#include "Basics.h" +#include "Matrix.h" namespace Microsoft { namespace MSR { namespace CNTK { @@ -267,8 +268,12 @@ public: void SetAxisName(const std::wstring& name) { m_axisName = name; } void SetUniqueAxisName(std::wstring name) // helper for constructing { - static std::map nameIndices; - size_t index = nameIndices[name]++; + static atomic_ullong index = ATOMIC_VAR_INIT(0); + + // To make this function thread-safe, a global index is used instead of that is bound to a specific name. + // However, this means that the index for a specific name is not continuously. + atomic_fetch_add(&index, (unsigned long long int) 1); + if (index > 0) name += msra::strfun::wstrprintf(L"%d", (int)index); SetAxisName(name);