!6810 [MD] Fix transforms train failed issue

Merge pull request !6810 from xiefangqi/xfq_fix_uniqueid_issue
This commit is contained in:
mindspore-ci-bot 2020-09-24 14:29:24 +08:00 committed by Gitee
commit 2e071243b3
2 changed files with 22 additions and 2 deletions

View File

@ -30,6 +30,8 @@ namespace mindspore {
namespace dataset {
std::unique_ptr<Services> Services::instance_ = nullptr;
std::once_flag Services::init_instance_flag_;
std::set<std::string> Services::unique_id_list_ = {};
std::mutex Services::unique_id_mutex_;
#if !defined(_WIN32) && !defined(_WIN64) && !defined(__ANDROID__) && !defined(ANDROID)
std::string Services::GetUserName() {
@ -52,8 +54,23 @@ std::string Services::GetUniqueID() {
std::mt19937 gen = GetRandomDevice();
std::uniform_int_distribution<uint32_t> dist(0, kStr.size() - 1);
char buffer[UNIQUEID_LEN];
for (int i = 0; i < UNIQUEID_LEN; i++) {
buffer[i] = kStr[dist(gen)];
{
std::unique_lock<std::mutex> lock(unique_id_mutex_);
while (true) {
auto ret = memset_s(buffer, UNIQUEID_LEN, 0, UNIQUEID_LEN);
if (ret != 0) {
MS_LOG(ERROR) << "memset_s error, errorno(" << ret << ")";
return std::string("");
}
for (int i = 0; i < UNIQUEID_LEN; i++) {
buffer[i] = kStr[dist(gen)];
}
if (unique_id_list_.find(std::string(buffer, UNIQUEID_LEN)) != unique_id_list_.end()) {
continue;
}
unique_id_list_.insert(std::string(buffer, UNIQUEID_LEN));
break;
}
}
return std::string(buffer, UNIQUEID_LEN);
}

View File

@ -19,6 +19,7 @@
#include <algorithm>
#include <memory>
#include <mutex>
#include <set>
#include <string>
#include <vector>
#include "minddata/dataset/util/memory_pool.h"
@ -97,6 +98,8 @@ class Services {
private:
static std::once_flag init_instance_flag_;
static std::unique_ptr<Services> instance_;
static std::set<std::string> unique_id_list_;
static std::mutex unique_id_mutex_;
// A small pool used for small objects that last until the
// Services Manager shuts down. Used by all sub-services.
std::shared_ptr<MemoryPool> pool_;