tmembug.nim 1017 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import std / [atomics, strutils, sequtils]
  2. type
  3. BackendMessage* = object
  4. field*: seq[int]
  5. var
  6. chan1: Channel[BackendMessage]
  7. chan2: Channel[BackendMessage]
  8. chan1.open()
  9. chan2.open()
  10. proc routeMessage*(msg: BackendMessage) =
  11. discard chan2.trySend(msg)
  12. var
  13. recv: Thread[void]
  14. stopToken: Atomic[bool]
  15. proc recvMsg() =
  16. while not stopToken.load(moRelaxed):
  17. let resp = chan1.tryRecv()
  18. if resp.dataAvailable:
  19. routeMessage(resp.msg)
  20. echo "child consumes ", formatSize getOccupiedMem()
  21. createThread[void](recv, recvMsg)
  22. const MESSAGE_COUNT = 100
  23. proc main() =
  24. let msg: BackendMessage = BackendMessage(field: (0..500).toSeq())
  25. for j in 0..0: #100:
  26. echo "New iteration"
  27. for _ in 1..MESSAGE_COUNT:
  28. chan1.send(msg)
  29. echo "After sending"
  30. var counter = 0
  31. while counter < MESSAGE_COUNT:
  32. let resp = recv(chan2)
  33. counter.inc
  34. echo "After receiving ", formatSize getOccupiedMem()
  35. stopToken.store true, moRelaxed
  36. joinThreads(recv)
  37. main()