aboutsummaryrefslogtreecommitdiff
path: root/examples/learn/36-hold.zc
blob: 6052129a7d87164a9c30de896582a24a29afdc01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace;
include rt, actor;
typecheck on;

// one counter for every actor. only the functions written in the block can touch it,
// and each call holds the block's lock, so a read and the write after it can't interleave.
hold {
	shared let hits = 0;
	fn hit() { let seen = hits; hits = seen + 1; return seen; }
	fn total() { return hits; }
}

int work(int n) { let i = 0; while (i < n) { hit(); i++; } return n; }

fn main() {
	actor.Actor<int,int>* a = rt.new(actor.Actor<int,int>), b = rt.new(actor.Actor<int,int>);
	a.spawn(work); b.spawn(work);
	a.send(1000); b.send(1000);
	a.recv(fn(int n) { return 0; }); b.recv(fn(int n) { return 0; });
	a.await(); b.await();
	rt.put_n(total()); rt.put_c(10);
	a.close(); b.close();
	return 0;
}