ttryrecv.nim 563 B

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