The fiber module provides OS-indepedent lightweight threads aka fibers.
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.
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 );
assert( counter == 0 );
derived.call();
assert( counter == 2, "Derived fiber increment." );
composed.call();
assert( counter == 6, "First composed fiber increment." );
counter += 16;
assert( counter == 22, "Calling context increment." );
composed.call();
assert( counter == 30, "Second composed fiber increment." );
// since each fiber has run to completion, each should have state TERM
assert( derived.state == Fiber.State.TERM );
assert( composed.state == Fiber.State.TERM );
Initializes a fiber object which is associated with a static D function.
void function() fn
| The fiber function. |
size_t sz
| The stack size for this fiber. |
size_t guardPageSize
| size of the guard page to trap fiber's stack overflows. Beware that using this will increase the number of mmaped regions on platforms using mmap so an OS-imposed limit may be hit. |
Initializes a fiber object which is associated with a dynamic D function.
void delegate() dg
| The fiber function. |
size_t sz
| The stack size for this fiber. |
size_t guardPageSize
| size of the guard page to trap fiber's stack overflows. Beware that using this will increase the number of mmaped regions on platforms using mmap so an OS-imposed limit may be hit. |
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.
Rethrow rethrow
| Rethrow any unhandled exception which may have caused this fiber to terminate. |
Flag to control rethrow behavior of call
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.
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.
The TERM state is set when a fiber terminates. Once a fiber terminates, it must be reset before it may be called again.
Gets the current state of this fiber.
Forces a context switch to occur away from the calling fiber.
Forces a context switch to occur away from the calling fiber and then throws obj in the calling fiber.
Throwable t
| The object to throw. |
Provides a reference to the calling fiber or null if no fiber is currently active.
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/core_thread_fiber.html