Fibers

Fibers are a way to implemented concurrency in a cooperative fashion. The class Fiber is defined in the module core.thread.

The basic idea is that when a fiber has nothing to do or waits for more input, it actively gives away its possibility to execute instructions by calling Fiber.yield(). The parent context gains control again but the fiber's state - all variables on the stack - are saved. The fiber can then be resumed and will continue at the instruction right after it called Fiber.yield(). Magic? Yes.

void foo() {
    writeln("Hello");
    Fiber.yield();
    writeln("World");
}
// ...
auto f = new Fiber(&foo);
f.call(); // Prints Hello
f.call(); // Prints World

This feature can be used to implement concurrency where multiple fibers cooperatively share a single core. The advantage of fibers compared to threads is that their resource usage is lower because no context switching is involved.

A very good usage of this technique can be seen in the vibe.d framework which implements non-blocking (or asynchronous) I/O operations in terms of fibers leading to much cleaner code.

In-depth

rdmd playground.d