aboutsummaryrefslogtreecommitdiff
path: root/test/threads.tm
blob: 33685103422d920641b1320ec4fe9c66820c14a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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!