Cyber supports fibers as a concurrency mechanism. There are plans to support preemptive concurrency with async/await as well as multithreading.
Fibers in Cyber allow representing execution contexts as first-class values. They contain their own call stack and program counters. Fibers by themselves do not enable parallelism.
coinit creates a new fiber from a function call syntax. Using
coyield inside a function pauses the current fiber and execution is returned to the fiber that invoked
count = 0 func foo(): count += 1 coyield count += 1 fiber = coinit foo() print count -- '0' coresume fiber print count -- '1' coresume fiber print count -- '2'
coyield can be used anywhere in a fiber’s call stack.
func foo(): count += 1 bar() func bar(): -- Nested coyield in call stack. coyield count += 1 fiber = coinit foo() coresume fiber
coresume also returns the resulting value. In a future version of Cyber, you will be able to yield back results and pass values back when resuming.
func foo(): return 123 fiber = coinit foo() print(coresume fiber) -- '123'
Fiber.status() to get the current state of the fiber.
func foo(): coyield print 'done' fiber = coinit foo() print fiber.status() -- '#paused' coresume fiber print fiber.status() -- '#paused' coresume fiber print fiber.status() -- '#done'
The main execution context is a fiber as well. Once the main fiber has finished, the VM is done and control is returned to the host.