2013-08-22 30 views
1
void BalanceTransfer(Account &FromAccount, Account &ToAccount, double amount) 
{ 
    FromAccount.Substract(amount); 
    ToAccount.Add(amount); 
    //What is the best way to protect this function if there are 100 threads calling this function for 1000's of accounts... 
} 

回答

1

在每個帳戶互斥。在交易之前,鎖定兩個互斥體(當然,之後再釋放它們)。鎖定順序很重要;爲避免死鎖,您需要定義的鎖定順序,例如,始終先用最低的賬號鎖定賬戶。

+0

感謝您的答覆。好的解決方案 – srinivas1983