When reading the concurrency section on fibers I had some trouble understanding why fiber.reset() and fiber.status() was called in example only defining a fiber called task. After reading through some documentation and testing in the playground I have come to the conclusion that it is probably an error of the documentation.
The examples in question are the following:
|
### Reset state. |
|
To reset a fiber to its initial state, invoke `reset()`. *Planned Feature* |
|
When reset, the existing stack is unwinded, the program counter returns to the starting point, and the state is set to `.init`: |
|
```cy |
|
func fib(n int) int: |
|
coyield n |
|
if n < 2: |
|
return n |
|
return fib(n - 1) + fib(n - 2) |
|
|
|
var task = coinit(fib, 10) |
|
|
|
-- Progress the fiber... |
|
print(coresume task) -- Prints "10" |
|
print(coresume task) -- Prints "9" |
|
print(coresume task) -- Prints "8" |
|
|
|
-- Reset back to the start with the `.init` state. |
|
fiber.reset() |
|
print(coresume task) -- Prints "10" |
|
``` |
|
|
|
### Rebinding arguments. |
|
Arguments attached to the fiber can be rebinded with a different set of values. *Planned Feature* |
|
This allows fiber reuse, instead of creating a new fiber: |
|
```cy |
|
var task = coinit(fib, 10) |
|
|
|
-- Run task to completion. |
|
var res = 0 |
|
while fiber.status() != .done: |
|
res = coresume fiber |
|
print res |
|
|
|
fiber.reset() |
|
fiber.bindArgs(20) |
|
|
|
-- Run task again with the new argument... |
|
``` |
When reading the concurrency section on fibers I had some trouble understanding why
fiber.reset()andfiber.status()was called in example only defining a fiber calledtask. After reading through some documentation and testing in the playground I have come to the conclusion that it is probably an error of the documentation.The examples in question are the following:
cyber/docs/docs.md
Lines 2364 to 2402 in f3bda25