Fibers and Asynchronous I/O

39.1 Fibers (core.thread.Fiber)

Fibers are cooperative coroutines with their own stack. They're the basis of std.concurrency.Generator and vibe.d's async model.

import core.thread : Fiber;
auto fib = new Fiber({
    foreach (i; 0 .. 3) { writeln("fiber step ", i); Fiber.yield(); }
});
while (fib.state != Fiber.State.TERM) { fib.call(); writeln("back in main"); }

39.2 vibe.d Async I/O

vibe.d (and its eventcore library) runs fibers on an event loop (epoll/kqueue/IOCP). Code looks synchronous but doesn't block threads:

Alternatives: photon (transparent fiber scheduling of blocking syscalls), hunt framework, during (io_uring).

import vibe.core.core, vibe.core.net;
void main()
{
    listenTCP(7000, (conn) @safe nothrow {
        try { conn.write(conn.readAll()); } catch (Exception) {}   // echo server
    });
    runApplication();
}