JH-Toolkit v1.4.1
An engineering-oriented C++20 toolkit with duck-typed concepts, static design, async coroutines, and semantic containers — header-only, RTTI-free, and concurrency-friendly.
Loading...
Searching...
No Matches
jh::async::fiber Class Referencefinal

Coroutine-based fiber providing manual suspension and resumption. More...

#include <jh/asynchronous/fiber.h>

Classes

struct  promise_type

Public Member Functions

 fiber (std::coroutine_handle< promise_type > h) noexcept
 Constructs a fiber from a coroutine handle.
 ~fiber ()
 Destroys the fiber and its coroutine frame if valid.
 fiber (const fiber &)=delete
 Copy construction is disabled.
fiberoperator= (const fiber &)=delete
 Copy assignment is disabled.
 fiber (fiber &&other) noexcept
 Move constructor transferring ownership of the coroutine handle.
fiberoperator= (fiber &&other) noexcept
 Move assignment operator transferring ownership of the coroutine handle.
bool done () const noexcept
 Checks whether the fiber has reached its final suspend point.
bool resume ()
 Resumes execution of the fiber.

Public Attributes

std::coroutine_handle< promise_typeco_ro
 Handle to the coroutine.

Detailed Description

Coroutine-based fiber providing manual suspension and resumption.

A fiber models a manually advanced coroutine, conceptually equivalent to a thread blocked on std::condition_variable.wait():

execution is suspended until an explicit external resume request is issued. Unlike an OS thread, a fiber is extremely lightweight because it stores only a coroutine frame, not a stack.

Suspension inside the coroutine is performed using co_await resume_tag, which introduces a controlled wait point analogous to condition_variable.wait():

  • always suspends,
  • registers no scheduler,
  • resumes only when the caller explicitly invokes resume().

Exception propagation is disabled inside a fiber. Any uncaught exception results in a call to std::terminate(), matching the semantics of a thread whose entry function throws without being caught. This avoids continuing execution after a fatal error.

Behavior Characteristics

  • Lightweight coroutine frame instead of an OS thread.
  • Manual control: external calls to resume() drive progress.
  • co_await resume_tag provides explicit suspension points.
  • Fatal errors end in std::terminate(), matching thread behavior.

Constructor & Destructor Documentation

◆ fiber() [1/3]

jh::async::fiber::fiber ( std::coroutine_handle< promise_type > h)
inlineexplicitnoexcept

Constructs a fiber from a coroutine handle.

The passed handle represents an allocated coroutine frame created by promise_type.get_return_object(). Ownership is adopted by the fiber instance, which is responsible for destroying the frame when no longer needed.

Parameters
hThe coroutine handle associated with this fiber.

◆ ~fiber()

jh::async::fiber::~fiber ( )
inline

Destroys the fiber and its coroutine frame if valid.

If the internal coroutine handle is non-null, its coroutine frame is destroyed via destroy(). This fully releases all memory associated with the coroutine.

◆ fiber() [2/3]

jh::async::fiber::fiber ( const fiber & )
delete

Copy construction is disabled.

A fiber represents exclusive ownership of a coroutine frame; copying the handle would produce double-destruction and is therefore prohibited.

◆ fiber() [3/3]

jh::async::fiber::fiber ( fiber && other)
inlinenoexcept

Move constructor transferring ownership of the coroutine handle.

Parameters
otherSource fiber whose handle will be moved from.

Member Function Documentation

◆ done()

bool jh::async::fiber::done ( ) const
inlinenodiscardnoexcept

Checks whether the fiber has reached its final suspend point.

A fiber is considered complete if:

  • it has no coroutine handle, or
  • its coroutine handle reports done() == true.
Returns
true if the coroutine is finished; otherwise false.

◆ operator=()

fiber & jh::async::fiber::operator= ( fiber && other)
inlinenoexcept

Move assignment operator transferring ownership of the coroutine handle.

Destroys the current coroutine frame if present, then adopts the frame from other. The source fiber is left in a null state.

Parameters
otherSource fiber.
Returns
*this.

◆ resume()

bool jh::async::fiber::resume ( )
inline

Resumes execution of the fiber.

If the fiber is already complete, the function returns false. Otherwise, it resumes the coroutine, advancing it until the next suspension point (co_await resume_tag) or final completion.

Returns
true if further progress is possible; false if the fiber has finished execution.

The documentation for this class was generated from the following file: