Created
December 17, 2015 22:05
-
-
Save Keruspe/909f1ecb8899253637c2 to your computer and use it in GitHub Desktop.
stdin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From e71bfae7f3a64f152cf7de9878f31e38b468ce1f Mon Sep 17 00:00:00 2001 | |
From: Marc-Antoine Perennou <Marc-Antoine@Perennou.com> | |
Date: Thu, 17 Dec 2015 23:05:19 +0100 | |
Subject: [PATCH] lots of fixes lol | |
Signed-off-by: Marc-Antoine Perennou <Marc-Antoine@Perennou.com> | |
--- | |
foo.cc | 34 +++++++++++++++++++++++----------- | |
1 file changed, 23 insertions(+), 11 deletions(-) | |
diff --git a/foo.cc b/foo.cc | |
index 3a83092..3fe87de 100644 | |
--- a/foo.cc | |
+++ b/foo.cc | |
@@ -23,19 +23,21 @@ private: | |
static Bank* global_instance; | |
unsigned int accounts[4]; | |
mutex bank_lock; | |
+ unsigned int do_get_balance(unsigned int account_id); | |
+ void do_set_balance(unsigned int account_id, unsigned int amount); | |
Bank(); | |
}; | |
-Bank* Bank::global_instance = 0; | |
+Bank* Bank::global_instance = nullptr; | |
// Singleton instance of the bank | |
Bank* Bank::instance() { | |
- if (global_instance == 0) { | |
- // avoid multiple creations of a bank instance | |
- global_lock.lock(); | |
+ // avoid multiple creations of a bank instance | |
+ global_lock.lock(); | |
+ if (global_instance == nullptr) { | |
global_instance = new Bank; | |
- global_lock.unlock(); | |
} | |
+ global_lock.unlock(); | |
return global_instance; | |
} | |
@@ -50,30 +52,40 @@ Bank::Bank() { | |
unsigned int Bank::get_balance(unsigned int account_id) { | |
// the lock_guard releases the lock when it gets out of scope | |
lock_guard<mutex> lock(bank_lock); | |
+ return do_get_balance(account_id); | |
+} | |
+ | |
+unsigned int Bank::do_get_balance(unsigned int account_id) { | |
random_wait(); | |
return accounts[account_id]; | |
} | |
/* sets the current balance for account_id */ | |
void Bank::set_balance(unsigned int account_id, unsigned int amount) { | |
- // an account should never have a negative balance | |
- assert(amount >= 0); | |
lock_guard<mutex> lock(bank_lock); | |
+ do_set_balance(account_id, amount); | |
+} | |
+ | |
+void Bank::do_set_balance(unsigned int account_id, unsigned int amount) { | |
+ // an account should never have a negative balance | |
random_wait(); | |
accounts[account_id] = amount; | |
} | |
/* transfers the specified amount from account_id_1 to account_id_2 */ | |
void Bank::transfer(unsigned int account_id_1, unsigned int account_id_2, unsigned int amount) { | |
- unsigned int balance_1 = get_balance(account_id_1); | |
+ lock_guard<mutex> lock(bank_lock); | |
+ unsigned int balance_1 = do_get_balance(account_id_1); | |
+ // ensure we have enough founds | |
+ assert(balance_1 > amount); | |
random_wait(); | |
- unsigned int balance_2 = get_balance(account_id_2); | |
+ unsigned int balance_2 = do_get_balance(account_id_2); | |
random_wait(); | |
- set_balance(account_id_1, balance_1 - amount); | |
+ do_set_balance(account_id_1, balance_1 - amount); | |
random_wait(); | |
- set_balance(account_id_2, balance_2 + amount); | |
+ do_set_balance(account_id_2, balance_2 + amount); | |
} | |
void f1() { | |
-- | |
2.6.4 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment