aboutsummaryrefslogtreecommitdiff
path: root/examples/learn
diff options
context:
space:
mode:
authorGravatar zinc <zinc@downsta.ir> 2026-10-15 17:03:03 +0330
committerGravatar zinc <zinc@downsta.ir> 2026-10-15 17:03:03 +0330
commit7cb983bbea98d2038b2a8015a6d1f1afd40991c9 (patch)
treeec3d88a15801207fc6d8d3c6d142617ffec85460 /examples/learn
parentmerge work/borrow: borrow returns before definition, borrows clause, borrow m... (diff)
parentbench: rss probe path fix (diff)
merge work/actors: wasm seed bootstrap (seed/MANIFEST), shared copy-on-write actor memory, top-level hold blocks on all backends and lsp
Diffstat (limited to 'examples/learn')
-rw-r--r--examples/learn/36-hold.out1
-rw-r--r--examples/learn/36-hold.zc24
2 files changed, 25 insertions, 0 deletions
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<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;
+}