🐎 reduced time that Save() needs lock

This commit is contained in:
EntireTwix 2021-04-04 17:20:04 -07:00
parent 80b7daad96
commit 18424c64c3
2 changed files with 12 additions and 6 deletions

View file

@ -97,17 +97,23 @@ public:
std::ofstream user_save("users.json"); std::ofstream user_save("users.json");
Json::Value temp; Json::Value temp;
std::vector<std::string> temp_names;
temp_names.reserve(users.size());
{ {
std::unique_lock<std::shared_mutex> lock{save_lock}; //grabbing it from any busy add/del opperations std::unique_lock<std::shared_mutex> lock{save_lock}; //grabbing it from any busy add/del opperations
for (const auto &u : users) for (const auto &u : users)
{ {
//we know it contains this key but we call this func to grab mutex temp_names.push_back(u.first);
users.if_contains(u.first, [&temp, &u](const User &u_val) {
temp[u.first] = u_val.Serialize();
});
} }
} }
for (const std::string &s : temp_names)
{
users.if_contains(s, [&temp, &s](const User &u) {
temp[s] = u.Serialize();
});
}
writer->write(temp, &user_save); writer->write(temp, &user_save);
user_save.close(); user_save.close();
} }

View file

@ -7,9 +7,9 @@
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc != 4) if (argc != 3)
{ {
std::cerr << "Usage: sudo ./bank <admin password> <saving frequency in minutes> <threads>\n"; std::cerr << "Usage: sudo ./bank <admin password> <saving frequency in minutes>\n";
return 0; return 0;
} }
if (geteuid() != 0) if (geteuid() != 0)