HadesLang Doc
Search…
Channels
Hades also offers Go like channels via std:sync. This is useful for operations where one needs to sync two actors. Different from Go, in Hades channels can be used to connect multiple actors together. Every actor can send and every actor can receive (except if specified otherwise). One can send everything over a channel. Internally, the channel itself is an independent actor that manages member PIDs and messages.

Example - Send, async receive

This is the default setting. Here, everyone receives every message async.
1
with Channel from std:sync
2
with console from std:io
3
4
var chan = Channel()
5
6
func worker(worker, channel)
7
while(true)
8
//channel.get() is blocking
9
//channel.get() is also a fixed function that has state in the receiving process
10
var text = channel.get()
11
console.out(text)
12
end
13
end
14
15
var worker1 = spawn({_ => worker(1, chan)})
16
var worker2 = spawn({_ => worker(2, chan)})
17
18
//disable sending for other actors
19
chan.onlyReceive()
20
//...except for us
21
chan.allowSend(self())
22
23
//since the channel is just a wrapper around the send function,
24
//one need to manually register the pid to the channel
25
chan.register(worker1)
26
chan.register(worker2)
27
28
//Channel just overrides the shift operator
29
//One could also just do chan.send("Hello")
30
chan << "Hello"
31
chan << "World"
32
33
console.in() //block
34
35
/*
36
Output:
37
Hello
38
Hello
39
World
40
World
41
*/
Copied!

Example - First come, first serve (FCFS) channel

1
with Channel from std:sync
2
with console from std:io
3
4
var chan = Channel(mode=:fcfs)
5
6
func worker(worker, channel)
7
while(true)
8
var text = channel.get() //channel.get is blocking
9
console.out("Worker " + worker + ":" + text)
10
end
11
end
12
13
var worker1 = spawn({_ => worker(1, chan)})
14
var worker2 = spawn({_ => worker(2, chan)})
15
16
chan.register(worker1)
17
chan.register(worker2)
18
19
chan << "Hello"
20
chan << "World"
21
chan << "Foo"
22
chan << "Bar"
23
24
console.in()
Copied!

Example - Send to all, wait until everyone received

Here, the channel waits until everyone received the message to exit the blocking channel.get call.
1
with Channel from std:sync
2
with console from std:io
3
4
var chan = Channel(mode=:waitForAll)
5
6
func worker(worker, channel)
7
channel.get()
8
console.out("Worker " + worker + " received the message")
9
end
10
11
var worker1 = spawn({_ => worker(1, chan)})
12
var worker2 = spawn({_ => worker(2, chan)})
13
var worker3 = spawn({_ => worker(3, chan)})
14
15
chan.register([worker1, worker2, worker3])
16
17
chan << :hello
18
//Workers would print to stdout at the same time
19
20
console.in()
Copied!

Example - Load balance

This will load balance between multiple actors.
1
with Channel from std:sync
2
with console from std:io
3
4
var chan = Channel(mode=:loadBalance)
5
6
func worker(worker, channel)
7
while(true)
8
console.out("Worker " + worker + " " + channel.get())
9
end
10
end
11
12
var worker1 = spawn({_ => worker(1, chan)})
13
var worker2 = spawn({_ => worker(2, chan)})
14
var worker3 = spawn({_ => worker(3, chan)})
15
16
chan.register([worker1, worker2, worker3])
17
18
chan << :hello //Output: Worker 1 :hello
19
chan << :hello //Output: Worker 2 :hello
20
chan << :hello //Output: Worker 3 :hello
21
chan << :hello //Output: Worker 1 :hello
22
23
console.in()
Copied!
Last modified 2yr ago