Fix waiting on coroutines (#5463)
Public coroutine API returns a safe reference to specific running coroutine. Fixes bug where multiple coroutines executing same procedure would identify as same coroutine. Greatly optimizes `alive()` (and as a result of that `wait()`) calls. Coroutine struct is allocated together with stack as memory unmanaged by GC.
This commit is contained in:
parent
34a3d40d18
commit
cd2721242a
3 changed files with 56 additions and 27 deletions
19
tests/coroutines/twait.nim
Normal file
19
tests/coroutines/twait.nim
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
discard """
|
||||
output: "Exit 1\nExit 2"
|
||||
"""
|
||||
import coro
|
||||
|
||||
var coro1: CoroutineRef
|
||||
|
||||
proc testCoroutine1() =
|
||||
for i in 0..<10:
|
||||
suspend(0)
|
||||
echo "Exit 1"
|
||||
|
||||
proc testCoroutine2() =
|
||||
coro1.wait()
|
||||
echo "Exit 2"
|
||||
|
||||
coro1 = coro.start(testCoroutine1)
|
||||
coro.start(testCoroutine2)
|
||||
run()
|
||||
Loading…
Add table
Add a link
Reference in a new issue