titerators.nim 667 B

1234567891011121314151617181920212223242526272829
  1. discard """
  2. target: "c"
  3. """
  4. import coro
  5. include system/timers
  6. var
  7. stackCheckValue = 1100220033
  8. numbers = newSeqOfCap[int](10)
  9. iterator theIterator(id: int, sleep: float): int =
  10. for i in 0..<5:
  11. yield 10 * id + i
  12. suspend(sleep)
  13. proc theCoroutine(id: int, sleep: float32) =
  14. for n in theIterator(id, sleep):
  15. numbers.add(n)
  16. var start = getTicks()
  17. start(proc() = theCoroutine(1, 0.01))
  18. start(proc() = theCoroutine(2, 0.011))
  19. run()
  20. var executionTime = getTicks() - start
  21. doAssert(stackCheckValue == 1100220033, "Thread stack got corrupted")
  22. doAssert(numbers == @[10, 20, 11, 21, 12, 22, 13, 23, 14, 24], "Coroutines executed in incorrect order")