diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2024-06-06 00:52:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-06-06 00:52:01 +0200 |
commit | 69d0b73d667c4be9383f29cda3f70e411995d9af (patch) | |
tree | 080d98c6c02c1e22a484dacbb15930e95c644c38 /tests/alloc/tmembug.nim | |
parent | 87e56cabbb4d0d7326c37799d34d10e395a970fc (diff) | |
download | Nim-69d0b73d667c4be9383f29cda3f70e411995d9af.tar.gz |
fixes #22510 (#23100)
Diffstat (limited to 'tests/alloc/tmembug.nim')
-rw-r--r-- | tests/alloc/tmembug.nim | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/alloc/tmembug.nim b/tests/alloc/tmembug.nim new file mode 100644 index 000000000..63b51ec5d --- /dev/null +++ b/tests/alloc/tmembug.nim @@ -0,0 +1,54 @@ +discard """ + joinable: false +""" + +import std / [atomics, strutils, sequtils] + +type + BackendMessage* = object + field*: seq[int] + +var + chan1: Channel[BackendMessage] + chan2: Channel[BackendMessage] + +chan1.open() +chan2.open() + +proc routeMessage*(msg: BackendMessage) = + discard chan2.trySend(msg) + +var + recv: Thread[void] + stopToken: Atomic[bool] + +proc recvMsg() = + while not stopToken.load(moRelaxed): + let resp = chan1.tryRecv() + if resp.dataAvailable: + routeMessage(resp.msg) + echo "child consumes ", formatSize getOccupiedMem() + +createThread[void](recv, recvMsg) + +const MESSAGE_COUNT = 100 + +proc main() = + let msg: BackendMessage = BackendMessage(field: (0..500).toSeq()) + for j in 0..0: #100: + echo "New iteration" + + for _ in 1..MESSAGE_COUNT: + chan1.send(msg) + echo "After sending" + + var counter = 0 + while counter < MESSAGE_COUNT: + let resp = recv(chan2) + counter.inc + echo "After receiving ", formatSize getOccupiedMem() + + stopToken.store true, moRelaxed + joinThreads(recv) + +main() |