use struct-level static instead of local static as work around to ensure thread-safety in VS2013

This commit is contained in:
Zhou Wang 2016-09-15 15:44:45 +02:00
Родитель bee217707c
Коммит 7449a77618
4 изменённых файлов: 28 добавлений и 2 удалений

Просмотреть файл

@ -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 \

Просмотреть файл

@ -67,6 +67,7 @@
<ClCompile Include="fileutil.cpp" />
<ClCompile Include="Globals.cpp" />
<ClCompile Include="MPIWrapper.cpp" />
<ClCompile Include="Sequences.cpp" />
<ClCompile Include="TimerUtility.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />

Просмотреть файл

@ -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<std::wstring, size_t> 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

Просмотреть файл

@ -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;
}}}