aboutsummaryrefslogtreecommitdiff
path: root/test/threads.tm
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2024-08-11 14:47:34 -0400
committerBruce Hill <bruce@bruce-hill.com>2024-08-11 14:47:34 -0400
commit2ecb5fe885042ca6c25ee0a3e3da070ddec9e07e (patch)
treecdd7c7d1d51982d4074f76a1bccc522fbc8b5eee /test/threads.tm
parent3bf8ea8e12a2728bf63968ca7b42359b089e318b (diff)
Add channels and threads
Diffstat (limited to 'test/threads.tm')
-rw-r--r--test/threads.tm32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/threads.tm b/test/threads.tm
new file mode 100644
index 00000000..33685103
--- /dev/null
+++ b/test/threads.tm
@@ -0,0 +1,32 @@
+enum Job(Increment(x:Int), Decrement(x:Int))
+
+func main():
+ jobs := |:Job|
+ results := |:Int|
+ >> thread := Thread.new(func():
+ //! In another thread!
+ while yes:
+ when jobs:pop() is Increment(x):
+ >> results:push(x+1)
+ is Decrement(x):
+ >> results:push(x-1)
+ )
+
+ >> jobs:push(Increment(5))
+ >> jobs:push(Decrement(100))
+
+ >> results:pop()
+ = 6
+
+ >> jobs:push(Increment(1000))
+ >> results:pop()
+ = 99
+
+ >> results:pop()
+ = 1001
+
+ //! Canceling...
+ >> thread:cancel()
+ //! Joining...
+ >> thread:join()
+ //! Done!