tmembug.nim 1.0 KB

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