Cooperative multitasking written in swift with only one exception.
.package(url:"https://github.com/swiftstack/fiber.git",.branch("dev"))fiber{print("hello from fiber 1")fiber{print("hello from fiber 2")
yield()print("bye from fiber 2")}print("no, you first")
yield()print("bye from fiber 1")}FiberLoop.main.run()
// hello from fiber 1
// hello from fiber 2
// no, you first
// bye from fiber 2
// bye from fiber 1varchannel=Channel<Int>()fiber{whilelet value = channel.read(){print("read: \(value)")}print("read: the channel is closed.")}fiber{foriin0..<5{
channel.write(i)}
channel.close()}
// read: 0
// read: 1
// read: 2
// read: 3
// read: 4
// read: the channel is closed.import Time
fiber{fiber{sleep(until:.now.advanced(by:.milliseconds(2)))print("fiber 2 woke up")}sleep(until:.now.advanced(by:.milliseconds(1)))print("fiber 1 woke up")}FiberLoop.main.run(until:.now.advanced(by:.milliseconds(5)))
// fiber 1 woke up
// fiber 2 woke up