View source code
Display the source code in core/thread.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Class core.thread.Fiber

This class provides a cooperative concurrency mechanism integrated with the threading and garbage collection functionality. Calling a fiber may be considered a blocking operation that returns when the fiber yields (via Fiber.yield()). Execution occurs within the context of the calling thread so synchronization is not necessary to guarantee memory visibility so long as the same thread calls the fiber each time. Please note that there is no requirement that a fiber be bound to one specific thread. Rather, fibers may be freely passed between threads so long as they are not currently executing. Like threads, a new fiber thread may be created using either derivation or composition, as in the following example.

Inherits from


Name Description
this Initializes a fiber object which is associated with a static D function.
this Initializes a fiber object which is associated with a dynamic D function.


Name Type Description
state [get] Fiber.State Gets the current state of this fiber.


Name Description
call Transfers execution to this fiber object. The calling context will be suspended until the fiber calls Fiber.yield() or until it terminates via an unhandled exception.
getThis Provides a reference to the calling fiber or null if no fiber is currently active.
reset Resets this fiber so that it may be re-used, optionally with a new function/delegate. This routine should only be called for fibers that have terminated, as doing otherwise could result in scope-dependent functionality that is not executed. Stack-based classes, for example, may not be cleaned up properly if a fiber is reset before it has terminated.
yield Forces a context switch to occur away from the calling fiber.
yieldAndThrow Forces a context switch to occur away from the calling fiber and then throws obj in the calling fiber.
factory Create instance of class specified by the fully qualified name classname. The class must either have no constructors or have a default constructor.
opCmp Compare with another Object obj.
opEquals Test whether this is equal to o. The default implementation only compares by identity (using the is operator). Generally, overrides for opEquals should attempt to compare objects by their contents.
toHash Compute hash function for Object.
toString Convert Object to a human readable string.


Name Description
Rethrow Flag to control rethrow behavior of call
State A fiber may occupy one of three states: HOLD, EXEC, and TERM. The HOLD state applies to any fiber that is suspended and ready to be called. The EXEC state will be set for any fiber that is currently executing. And the TERM state is set when a fiber terminates. Once a fiber terminates, it must be reset before it may be called again.


Status registers are not saved by the current implementations. This means floating point exception status bits (overflow, divide by 0), rounding mode and similar stuff is set per-thread, not per Fiber!


On ARM FPU registers are not saved if druntime was compiled as ARM_SoftFloat. If such a build is used on a ARM_SoftFP system which actually has got a FPU and other libraries are using the FPU registers (other code is compiled as ARM_SoftFP) this can cause problems. Druntime must be compiled as ARM_SoftFP in this case.


class DerivedFiber : Fiber
        super( &run );

private :
    void run()
        printf( "Derived fiber running.\n" );

void fiberFunc()
    printf( "Composed fiber running.\n" );
    printf( "Composed fiber running.\n" );

// create instances of each type
Fiber derived = new DerivedFiber();
Fiber composed = new Fiber( &fiberFunc );

// call both fibers once;;
printf( "Execution returned to calling context.\n" );;

// since each fiber has run to completion, each should have state TERM
assert( derived.state == Fiber.State.TERM );
assert( composed.state == Fiber.State.TERM );


Sean Kelly, Walter Bright, Alex Rønne Petersen, Martin Nowak


Distributed under the Boost Software License 1.0. (See accompanying file LICENSE)