The programming model of vibe.d
The key to the scalability of vibe.d is the use of asynchronous I/O in combination with fibers.
What is a fiber?
Modern operating systems implement preemptive multitasking. A thread runs until its time slice is exhausted or it must wait for an I/O operation. Then the kernel chooses a different thread to run. Ultimately, the scheduler in the kernel controls the threads.
In contrast, fibers are a form of cooperative multitasking. As the name implies, cooperative multitasking requires some help from the user functions. A function runs up to a point where the developer decides would be a good place to run another task. Usually, a library function named yield()
is called, which continues the execution of another function. This is best shown with an example. Here is a simplified version of the classic producer-consumer pattern:
import std.stdio; import std.math; import core.thread; private int goods; private bool exit; void producerFiber() { foreach (i; 0..3) ...