In the code hereunder, if do not put a long enough delay in the loop which calls chan.tryRecv(), the main thread will fail to read from chan and exit, is that the expected behavior of threadpools in Nim?
os
import threadpool
import strformat
var chan: Channel[string]
open(chan)
proc sayHello() =
for i in 0..10:
chan.send(fmt"Hello {i} !")
sleep(5)
proc sayBye() =
for i in 0..10:
chan.send(fmt"Bye {i} !")
sleep(10)
spawn sayHello()
spawn sayBye()
while true:
let tried = chan.tryRecv()
if tried.dataAvailable:
echo tried.msg
sleep(100)
else:
break
Does Nim implement something similar to the channel / select pattern in Go ?
It is useful for dealing with a plurality of incoming messages on many channels concurrently.
select {
case msg := <-messages:
fmt.Println("received message", msg)
case sig := <-signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}