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.

class Fiber ;

Constructors

NameDescription
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.

Properties

NameTypeDescription
state[get] Fiber.StateGets the current state of this fiber.

Methods

NameDescription
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.

Enums

NameDescription
Rethrow Flag to control rethrow behavior of call
State A fiber may occupy one of three states: HOLD, EXEC, and TERM.

Warning

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!

Warning

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.

Example

int counter;

class DerivedFiber : Fiber
{
    this()
    {
        super( &run );
    }

private :
    void run()
    {
        counter += 2;
    }
}

void fiberFunc()
{
    counter += 4;
    Fiber.yield();
    counter += 8;
}

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

writeln(counter); // 0

derived.call();
writeln(counter); // 2

composed.call();
writeln(counter); // 6

counter += 16;
writeln(counter); // 22

composed.call();
writeln(counter); // 30

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

Authors

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

License

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