tmembug2.nim 1.1 KB

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