From 24a21f7520ce91681e573833b7f33156e308964a Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 14:36:10 +0330 Subject: docs: hold blocks in LEARN.md (36-hold), spec 5.16, ACTOR.md; TODO: m:n scheduler and follow-ups --- examples/learn/36-hold.out | 1 + examples/learn/36-hold.zc | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 examples/learn/36-hold.out create mode 100644 examples/learn/36-hold.zc (limited to 'examples') diff --git a/examples/learn/36-hold.out b/examples/learn/36-hold.out new file mode 100644 index 0000000..8bd1af1 --- /dev/null +++ b/examples/learn/36-hold.out @@ -0,0 +1 @@ +2000 diff --git a/examples/learn/36-hold.zc b/examples/learn/36-hold.zc new file mode 100644 index 0000000..6052129 --- /dev/null +++ b/examples/learn/36-hold.zc @@ -0,0 +1,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* a = rt.new(actor.Actor), b = rt.new(actor.Actor); + 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; +} -- cgit v1.2.3