Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <thread>
- #include <stdio.h>
- using namespace std;
- class Account
- {
- public:
- Account(int balance): balance(balance) { }
- int getBalance() const
- {
- return balance;
- }
- void deposit(int amount)
- {
- balance += amount;
- }
- bool withdraw(int amount)
- {
- if (balance >= amount)
- {
- balance -= amount;
- return true;
- }
- return false;
- }
- private:
- int balance;
- };
- void client(int clientid, Account &account, int amount)
- {
- printf("Client %d balance: %d\n", clientid, account.getBalance());
- bool result = account.withdraw(amount);
- if (result)
- printf("Client %d withdraw %d OK\n", clientid, amount);
- else
- printf("Client %d withdraw %d FAILED\n", clientid, amount);
- printf("Client %d balance: %d\n", clientid, account.getBalance());
- }
- int main(int argc, char *argv[])
- {
- Account account(100);
- thread t1(client, 1, ref(account), 90);
- thread t2(client, 2, ref(account), 80);
- thread t3(client, 3, ref(account), 70);
- t1.join();
- t2.join();
- t3.join();
- cout << "Account balance " << account.getBalance()
- << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement