boost::capy::work_guard

RAII guard that keeps an executor's context from completing.

Synopsis

template<Executor Ex>
class work_guard;

Description

This class holds "work" on an executor, preventing the associated execution context's run() function from returning due to lack of work. It calls on_work_started() on construction and on_work_finished() on destruction, ensuring proper work tracking.

The guard is useful when you need to keep an execution context running while waiting for external events or when work will be posted later.

RAII Semantics

  • Construction calls `ex.on_work_started()`.

  • Destruction calls `ex.on_work_finished()` if `owns_work()`.

  • Copy construction creates a new work reference (calls `on_work_started()` again).

  • Move construction transfers ownership without additional calls.

Thread Safety

Distinct objects may be accessed concurrently. Access to a single object requires external synchronization.

Example

io_context ctx;

// Keep context running while we set things up
auto guard = make_work_guard(ctx);

std::thread t([&ctx]{ ctx.run(); });

// ... post work to ctx ...

// Allow context to complete when work is done
guard.reset();

t.join();

The executor is returned by reference, allowing callers to manage the executor's lifetime directly. This is essential in coroutine‐first designs where the executor often outlives individual coroutine frames.

Types

Name

Description

executor_type

The underlying executor type.

Member Functions

Name

Description

work_guard [constructor]

Constructors

~work_guard [destructor]

Destructor.

operator= [deleted]

Copy assignment operator

executor

Return the underlying executor by reference.

owns_work

Return whether the guard owns work.

reset

Release ownership of the work.

Non-Member Functions

Name

Description

make_work_guard

Create a work guard from an executor.

Template Parameters

Name Description

Ex

A type satisfying the Executor concept.

See Also

make_work_guard, Executor

Created with MrDocs