Advertisement
RobertDeMilo

RB5.18 Состояние гонки

Apr 18th, 2024
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.17 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdint>
  3. #include<vector>
  4. #include<future>
  5. #include "log_duration.h"
  6.  
  7. using namespace std;
  8.  
  9. struct Account
  10. {
  11.     int balance = 0;
  12.  
  13.     bool Spend(int value)
  14.     {
  15.         if (value <= balance)
  16.         {
  17.             balance -= value;
  18.             return true;
  19.         }
  20.         return false;
  21.     }
  22. };
  23.  
  24. int SpendMoney(Account& account)
  25. {
  26.     int total_spent = 0;
  27.    
  28.     for (int i = 0; i < 100'000; ++i)
  29.    {
  30.        if (account.Spend(1))
  31.        {
  32.            ++total_spent;
  33.        }
  34.    }
  35.    return total_spent;
  36. }
  37.  
  38. int main()
  39. {
  40.    Account family_account{ 100'000 };
  41.  
  42.     auto husband = async(SpendMoney, ref(family_account));
  43.     auto wife = async(SpendMoney, ref(family_account));
  44.     auto son = async(SpendMoney, ref(family_account));
  45.     auto daughter = async(SpendMoney, ref(family_account));
  46.  
  47.     int spent = husband.get() + wife.get() + son.get() + daughter.get();
  48.  
  49.     cout << "Total spent: " << spent << endl
  50.         << "Balance: " << family_account.balance << endl;
  51.     /*cout << "Total spent: " << SpendMoney(family_account) << endl
  52.         << "Balance: " << family_account.balance << endl;*/
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement