8000 Implement Fiber.reset · Issue #85 · fubark/cyber · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Implement Fiber.reset #85
Open
Open
@Goju-Ryu

Description

@Goju-Ryu

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:

cyber/docs/docs.md

Lines 2364 to 2402 in f3bda25

### 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...
```

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0