🐎 made add/del user based on shared lock system for Save()

This commit is contained in:
EntireTwix 2021-04-02 23:55:31 -07:00
parent ea511c71b2
commit d2fcf4f863

View file

@ -15,14 +15,14 @@ private:
std::mutex>
users;
std::mutex size_users;
std::shared_mutex size_lock;
public:
std::string admin_pass;
bool AddUser(const std::string &name, std::string &&init_pass)
{
std::unique_lock<std::mutex> lock{size_users};
std::shared_lock<std::shared_mutex> lock{size_lock};
return users.try_emplace_l(
name, [](User &) {}, std::forward<std::string &&>(init_pass));
}
@ -31,7 +31,7 @@ public:
bool state = (admin_pass == attempt);
if (state)
{
std::unique_lock<std::mutex> lock{size_users};
std::shared_lock<std::shared_mutex> lock{size_lock};
state = users.try_emplace_l(
name, [](User &) {}, init_bal, std::forward<std::string &&>(init_pass));
}
@ -40,12 +40,10 @@ public:
bool DelUser(const std::string &name, const std::string &attempt)
{
std::unique_lock<std::mutex> lock{size_users};
return users.erase_if(name, [&attempt](const User &u) { return (attempt == u.password); });
}
bool AdminDelUser(const std::string &name, const std::string &attempt)
{
std::unique_lock<std::mutex> lock{size_users};
return users.erase_if(name, [this, &attempt](const User &) { return (admin_pass == attempt); });
}
@ -95,15 +93,16 @@ public:
std::ofstream user_save("users.json");
Json::Value temp;
//this gets access to the parallel map with the size being constant
std::unique_lock<std::mutex> lock{size_users};
for (const auto &u : users)
{
//this gets read access to prevent writes
users.if_contains(u.first, [&temp, &u](const User &u_val) {
temp[u.first] = u_val.Serialize();
})
std::unique_lock<std::shared_mutex> lock{size_lock}; //grabbing it from any busy add/del opperations
for (const auto &u : users)
{
temp[u.first] = u.second.Serialize();
}
std::cout << "dropped unique lock\n";
}
writer->write(temp, &user_save);
user_save.close();
}