Channels
Example - Send, async receive
with Channel from std:sync
with console from std:io
var chan = Channel()
func worker(worker, channel)
while(true)
//channel.get() is blocking
//channel.get() is also a fixed function that has state in the receiving process
var text = channel.get()
console.out(text)
end
end
var worker1 = spawn({_ => worker(1, chan)})
var worker2 = spawn({_ => worker(2, chan)})
//disable sending for other actors
chan.onlyReceive()
//...except for us
chan.allowSend(self())
//since the channel is just a wrapper around the send function,
//one need to manually register the pid to the channel
chan.register(worker1)
chan.register(worker2)
//Channel just overrides the shift operator
//One could also just do chan.send("Hello")
chan << "Hello"
chan << "World"
console.in() //block
/*
Output:
Hello
Hello
World
World
*/Example - First come, first serve (FCFS) channel
Example - Send to all, wait until everyone received
Example - Load balance
Last updated