From 7449a77618fd11220b3a7b0535457c5f7c07863b Mon Sep 17 00:00:00 2001 From: Zhou Wang Date: Thu, 15 Sep 2016 15:44:45 +0200 Subject: [PATCH] use struct-level static instead of local static as work around to ensure thread-safety in VS2013 --- Makefile | 1 + Source/Common/Common.vcxproj | 1 + Source/Common/Include/Sequences.h | 10 ++++++++-- Source/Common/Sequences.cpp | 18 ++++++++++++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 Source/Common/Sequences.cpp diff --git a/Makefile b/Makefile index f44cf3819..185ebd0a2 100644 --- a/Makefile +++ b/Makefile @@ -274,6 +274,7 @@ COMMON_SRC =\ $(SOURCEDIR)/Common/File.cpp \ $(SOURCEDIR)/Common/TimerUtility.cpp \ $(SOURCEDIR)/Common/fileutil.cpp \ + $(SOURCEDIR)/Common/Sequences.cpp \ MATH_SRC =\ $(SOURCEDIR)/Math/BatchNormalizationEngine.cpp \ diff --git a/Source/Common/Common.vcxproj b/Source/Common/Common.vcxproj index 6dce529d7..62933346a 100644 --- a/Source/Common/Common.vcxproj +++ b/Source/Common/Common.vcxproj @@ -67,6 +67,7 @@ + diff --git a/Source/Common/Include/Sequences.h b/Source/Common/Include/Sequences.h index b538eddc3..d08bea713 100644 --- a/Source/Common/Include/Sequences.h +++ b/Source/Common/Include/Sequences.h @@ -268,9 +268,11 @@ public: void SetAxisName(const std::wstring& name) { m_axisName = name; } void SetUniqueAxisName(std::wstring name) // helper for constructing { - // Unfortunatelly, the following initialization of local static variables is not thread-safe in VS2013. + // Unfortunatelly, initialization of local static variables is not thread-safe in VS2013. + // As workaround, it is moved to the struct level. + // Todo: when upgraded to VS2013, change back to use the local static mutex, and remove also Sequences.cpp. // The mutex is need to make access to nameIndices be thread-safe. - static std::mutex nameIndiciesMutex; + // static std::mutex nameIndiciesMutex; static std::map nameIndices; size_t index; @@ -577,6 +579,10 @@ private: // For now only a string meant for debugging. std::wstring m_axisName; + // The mutex to searilize the access to nameIndices in SetUniqueAxisName(). + // Todo: after upgraded to VS2015, move this static variable into SetUnqiueAxisName() as local static variable there. + static std::mutex nameIndiciesMutex; + public: // special accessor for sequence training --TODO: must be replaced by a different mechanism diff --git a/Source/Common/Sequences.cpp b/Source/Common/Sequences.cpp new file mode 100644 index 000000000..0ea304898 --- /dev/null +++ b/Source/Common/Sequences.cpp @@ -0,0 +1,18 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE.md file in the project root for full license information. +// + +#ifndef _CRT_SECURE_NO_WARNINGS +#define _CRT_SECURE_NO_WARNINGS // "secure" CRT not available on all platforms --add this at the top of all CPP files that give "function or variable may be unsafe" warnings +#endif + +#include "Sequences.h" + +namespace Microsoft { namespace MSR { namespace CNTK { + + // define the static member of MBLayout. + // Todo: After upgrade to VS2015, remove this when nameIndiciesMutex is moved into SetUnqiueAxisName as local static variable. + std::mutex MBLayout::nameIndiciesMutex; + +}}} \ No newline at end of file