ttryrecv.nim 541 B

123456789101112131415161718192021222324252627282930313233343536
  1. discard """
  2. outputsub: "channel is empty"
  3. """
  4. # bug #1816
  5. from random import rand
  6. from os import sleep
  7. type PComm = ptr Channel[int]
  8. proc doAction(outC: PComm) {.thread.} =
  9. for i in 0 ..< 5:
  10. sleep(rand(50))
  11. send(outC[], i)
  12. var
  13. thr: Thread[PComm]
  14. chan: Channel[int]
  15. open(chan)
  16. createThread[PComm](thr, doAction, addr(chan))
  17. while true:
  18. let (flag, x) = tryRecv(chan)
  19. if flag:
  20. echo("received from chan: " & $x)
  21. else:
  22. echo "channel is empty"
  23. break
  24. echo "Finished listening"
  25. joinThread(thr)
  26. close(chan)