summaryrefslogtreecommitdiff
path: root/demos/doubleup.apl
blob: d144f8fb24f822abba2d351ff6b48e500aee4000 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
⍝ Define the "double up" function
f←{
        (who message)←{1 ⍵}⍇⍬ ⍝ Recieve a message with no timeout
        (2×message)⍈who       ⍝ Reply with 2×message
        ∇⍵                    ⍝ Recurse
      }

⍝ Spawn it as a seperate thread
id←f&'double up'⊢⍬

⍝ Check it's status (id, name, mails in mailbox)
id ⎕threads 0 1 2

⍝ Check our own status
⎕self ⎕threads 0 1 2

⍝ Send a few messages to our own mailbox
'hello'⍈⎕self
(⍳2 2)⍈⎕self

⍝ Send a message to the double up thread
(⍳5)⍈id

⍝ Send a message to ourself again
'last one'⍈⎕self

⍝ Check our own status again
⎕self ⎕threads 0 1 2

⍝ We now have a few messages, but we want a specific one..
⍝ That is, the one where the sender = id
{id=⊃⍵:1 ⍵ ⋄ 0}⍇0

⍝ We might as well get the other messages as well
flush←{
        12::⍵ ⍝ Stop when we get a timeout error
        m←{1 ⍵}⍇0
        ∇⍵,⊂m
      }

flush ⍬