make FairProgOptions calls thread safe

This commit is contained in:
Alexey Rybalchenko
2017-06-26 12:29:19 +02:00
committed by Mohammad Al-Turany
parent b47cc8b29c
commit ac69607250
3 changed files with 35 additions and 25 deletions

View File

@@ -24,6 +24,7 @@
#include <iostream>
#include <fstream>
#include <iterator>
#include <mutex>
#include <tuple>
/*
@@ -77,6 +78,8 @@ class FairProgOptions
template<typename T>
T GetValue(const std::string& key) const
{
std::unique_lock<std::mutex> lock(fConfigMutex);
T val = T();
try
{
@@ -99,11 +102,32 @@ class FairProgOptions
return val;
}
// convert value to string that corresponds to the key
std::string GetStringValue(const std::string& key);
// Given a key, convert the variable value to string
std::string GetStringValue(const std::string& key)
{
std::unique_lock<std::mutex> lock(fConfigMutex);
std::string valueStr;
try
{
if (fVarMap.count(key))
{
valueStr = FairMQ::ConvertVariableValue<FairMQ::ToString>().Run(fVarMap.at(key));
}
}
catch (std::exception& e)
{
LOG(ERROR) << "Exception thrown for the key '" << key << "'";
LOG(ERROR) << e.what();
}
return valueStr;
}
int Count(const std::string& key) const
{
std::unique_lock<std::mutex> lock(fConfigMutex);
return fVarMap.count(key);
}
@@ -162,6 +186,8 @@ class FairProgOptions
std::map<std::string, FairMQ::severity_level> fSeverityMap;
po::options_description fVisibleOptions;
mutable std::mutex fConfigMutex;
std::string fVerbosityLevel;
bool fUseConfigFile;
boost::filesystem::path fConfigFile;