[dmd-concurrency] draft 7

Andrei Alexandrescu andrei at erdani.com
Mon Feb 1 11:46:58 PST 2010


(I assume you meant "BankAccount[string]".)

It all depends on the behavior of the types shared(BankAccount[string]), 
shared(BankAccount), and shared(string[]).

I didn't quite mention what happens to member arrays inside synchronized 
methods. I think I wouldn't have to, but ~= warrants it. Here's the 
deal: inside a synchronized method, a member of type T[] is shared(T)[], 
except if you want to take the address of the member, in which case it's 
shared(T[]). Yep, that's tail const. I haven't thought a great deal 
about this, but arrays of shared(T) may be appended to efficiently.

I have given zero thought to shared associative arrays, and this is a 
great time to start talking about it. For my money I think we need to 
have a whole separate type SharedMap(K, V).

Thanks for the question. Also thanks to Extrawurst and Sean for the 
feedback.


Andrei

Michel Fortin wrote:
> Hum, say I want to implement a synchronized Bank class as a collection of bank accounts:
> 
> 	synchronized class Bank {
> 		private BankAccount[name] accounts;
> 		private string[] log;
> 
> 		void addAccount(string name) {
> 			accounts[name] = new BankAccount();
> 			log ~= "account created";
> 		}
> 
> 		void transferMoney(string from, string to, uint amount) {
> 			BankAccount first = accounts[from];
> 			BankAccount second = accounts[to];
> 			synchronized (first, second) {
> 				first.withdraw(amount);
> 				second.deposit(amount);
> 				log ~= "money transfer successful";
> 			}
> 		}
> 
> 		void removeAccount(string name) {
> 			accounts.remove(name);
> 			log ~= "account removed";
> 		}
> 	}
> 
> Where am I going to need casts here?
> Also, which parts are going to be done using atomic operations?
> 


More information about the dmd-concurrency mailing list