Movatterモバイル変換


[0]ホーム

URL:


DLang LogoDLang Tour
Menu

Fibers

Fibers are a way to implement concurrencyin acooperative fashion. The classFiberis defined in the modulecore.thread.

The basic idea is that when a fiberhas nothing to do or waits for more input, itactively gives away its possibility toexecute instructions by callingFiber.yield().The parent context gains control again but thefiber's state - all variables on the stack - aresaved. The fiber can then be resumedand will continue at the instruction rightafterit calledFiber.yield(). Magic? Yes.

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

This feature can be used to implement concurrencywhere multiple fibers cooperatively share a singlecore. The advantage of fibers compared to threads isthat their resource usage is lower becauseno context switching is involved.

A very good usage of this technique can be seen inthevibe.d framework which implementsnon-blocking (or asynchronous) I/O operationsin terms of fibers leading to much cleanercode.

In-depth

rdmd playground.d


[8]ページ先頭

©2009-2025 Movatter.jp