From 18424c64c3b196db86e479651d78cc338cad4f41 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 4 Apr 2021 17:20:04 -0700 Subject: [PATCH] :racehorse: reduced time that Save() needs lock --- include/bank.hpp | 14 ++++++++++---- main.cpp | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/bank.hpp b/include/bank.hpp index 0cca459..ecdaf7e 100644 --- a/include/bank.hpp +++ b/include/bank.hpp @@ -97,17 +97,23 @@ public: std::ofstream user_save("users.json"); Json::Value temp; + std::vector temp_names; + temp_names.reserve(users.size()); { std::unique_lock lock{save_lock}; //grabbing it from any busy add/del opperations for (const auto &u : users) { - //we know it contains this key but we call this func to grab mutex - users.if_contains(u.first, [&temp, &u](const User &u_val) { - temp[u.first] = u_val.Serialize(); - }); + temp_names.push_back(u.first); } } + 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); user_save.close(); } diff --git a/main.cpp b/main.cpp index 17575aa..7681bc2 100644 --- a/main.cpp +++ b/main.cpp @@ -7,9 +7,9 @@ int main(int argc, char **argv) { - if (argc != 4) + if (argc != 3) { - std::cerr << "Usage: sudo ./bank \n"; + std::cerr << "Usage: sudo ./bank \n"; return 0; } if (geteuid() != 0)