Skip to content

Commit

Permalink
use unique_lock instead of atomic_ullong to keep axis naming unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
zhouwangzw committed Sep 15, 2016
1 parent d5d9767 commit 69f9115
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions Source/Common/Include/Sequences.h
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@

#include <vector>
#include <memory> // for shared_ptr
#include <atomic>
#include <mutex>
#include "Basics.h"
#include "Matrix.h"

@@ -268,11 +268,17 @@ struct MBLayout
void SetAxisName(const std::wstring& name) { m_axisName = name; }
void SetUniqueAxisName(std::wstring name) // helper for constructing
{
static atomic_ullong index = ATOMIC_VAR_INIT(0);
// Unfortunatelly, the following initialization of local static variables is not thread-safe in VS2013.
// The mutex is need to make access to nameIndices be thread-safe.
static std::mutex nameIndiciesMutex;
static std::map<std::wstring, size_t> nameIndices;
size_t index;

// 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);
// Use the block to make sure that nameIndiciesMutex is unlocked as soon as possible.
{
std::unique_lock<std::mutex> lock(nameIndiciesMutex);
index = nameIndices[name]++;
}

if (index > 0)
name += msra::strfun::wstrprintf(L"%d", (int)index);

0 comments on commit 69f9115

Please sign in to comment.