🐎 made SendFunds 10% faster

This commit is contained in:
EntireTwix 2021-04-01 20:13:47 -07:00
parent 1c7b2f67fe
commit ed04fb556f
2 changed files with 11 additions and 6 deletions

View file

@ -20,15 +20,21 @@ public:
const bool state = (password == attempt); const bool state = (password == attempt);
if (state) if (state)
{ {
password = new_pass; password == new_pass;
} }
return state; return state;
} }
friend bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt) friend bool SendFunds(User &a, User &b, uint_fast64_t amount, const std::string &attempt)
{ {
std::scoped_lock<std::mutex, std::mutex> lock{a.bal_lock, a.pass_lock}; bool state;
const bool state = (a.password == attempt) && (a.balance >= amount); {
std::lock_guard<std::mutex> lock{a.pass_lock};
state = (a.password == attempt);
}
std::scoped_lock<std::mutex, std::mutex> lock{a.bal_lock, b.bal_lock};
state = state && (a.balance >= amount);
if (state) if (state)
{ {
a.balance -= amount; a.balance -= amount;

View file

@ -5,11 +5,10 @@ int main()
{ {
User a(1000, "pass123"); User a(1000, "pass123");
User b(0, "pass123"); User b(0, "pass123");
std::cout << a.ChangePassword("pass123", "newpass123") << '\n'; a.ChangePassword("pass123", "newpass123");
std::cout << SendFunds(a, b, 250, "newpass123") << '\n'; SendFunds(a, b, 250, "newpass123");
a.GetBal(); a.GetBal();
a.VerifyPassword("newpass124"); a.VerifyPassword("newpass124");
std::cout << a.GetBal() << '\n';
return 0; return 0;
} }