I am getting error for the below code I am implementing thread safe write to files as below ::
class OpenFile
{
string fileName;
std::unique_lock<mutex> lck;
static map<string, unique_ptr<mutex>> fmap;
public :
OpenFile(string file) : fileName(file) {
try {
if(checkFile(file))
{
fmap.emplace(file, make_unique<mutex>());
}
else
{
unique_ptr<mutex> mu = std::move(fmap.find(file)->second);
lck=std::unique_lock<mutex>(*mu);
}
}
catch(string str)
{
cout << str << endl;
}
}
void writeToFile(string str)
{
string fname = fileName;
if (!checkFile(fname))
{
lck.lock();
ofstream ofile(fileName, ios::out);
ofile << "Writing to the file ";
ofile.close();
lck.unlock();
}
else
{
ofstream ofile(fileName, ios::out);
ofile << "Writing to the file ";
ofile.close();
}
}
string ReadFile()
{
string fname = fileName;
string line;
if (!checkFile(fname))
{
lck.lock();
ifstream ifile(fileName, ios::in);
getline(ifile, line);
ifile.close();
lck.unlock();
}
else
{
ifstream ifile(fileName, ios::in);
getline(ifile, line);
ifile.close();
}
return line;
}
OpenFile() = delete;
OpenFile& operator=(const OpenFile& o) = delete;
static bool checkFile(string& fname);
};
bool OpenFile::checkFile(string& fname)
{
if (find_if(fmap.begin(), fmap.end(), [&fname](pair<string, unique_ptr<mutex>> m)
{
if (m.first == fname)
return true;
else
return false;
}) == fmap.end())
{
return true;
}
else
return false;
}
Error from VS2015 ::
Description Project Line Suppression State
'bool OpenFile::checkFile::<lambda_2bd4a02838a970867505463f4b7b6b9e>::operator ()(const std::pair<std::string,std::unique_ptr<std::mutex,std::default_delete<_Ty>>>) const': cannot convert argument 1 from 'std::pair<const _Kty,_Ty>' to 'const std::pair<std::string,std::unique_ptr<std::mutex,std::default_delete<_Ty>>>' BoQian 43
Aucun commentaire:
Enregistrer un commentaire