Ejemplo n.º 1
0
        public void TwoConcurrentTransfersOnTheSameAccountsIsValid_CHESS()
        {
            var a = new Account(1);
            var b = new Account(2);
            var bank = new Bank();

            Parallel.Invoke(() => bank.Transfer(a, b, 100),
                () => bank.Transfer(b, a, 100));
        }
Ejemplo n.º 2
0
        public void TransferTo(Account other, double amount)
        {
            //Account a = (other.Id > this.Id) ? this : other;
            //Account b = (other.Id <= this.Id) ? this : other;

            lock (this)
            {
                Spin();
                lock (other)
                {
                    Spin();
                    this.Balance -= amount;
                    other.Balance += amount;
                }
            }
        }