C++ Academy · Lesson

Async Tasks and Awaiter Types

Compose async tasks with custom awaiter and promise types.

Lesson 3 of 414 steps

Async Tasks and Awaiter Types is a free C++ Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

Async Coroutines

An async task suspends on I/O and resumes when the operation completes. Combine co_await with asynchronous APIs for callback-free async code.

The Task Type Sketch

A Task wraps an async coroutine. It supports co_await for chaining.

template <typename T>
struct Task {
    struct promise_type;
    std::coroutine_handle<promise_type> handle;
    // ...
};

co_await Mechanics

When the compiler sees co_await expr, it expects expr to be an awaitable. An awaitable has three methods:

  • await_ready() — false to suspend, true to skip
  • await_suspend(handle) — what to do while suspended
  • await_resume() — value to give to the calling code

Awaitable Example: Sleep

An awaitable that schedules a resume after a delay.

struct Sleep {
    std::chrono::milliseconds duration;
    bool await_ready() const { return false; }
    void await_suspend(std::coroutine_handle<> h) {
        std::thread([h, this] {
            std::this_thread::sleep_for(duration);
            h.resume();
        }).detach();
    }
    void await_resume() {}
};

Task<void> example() {
    co_await Sleep{500ms};
    std::cout << "after 500ms";
}

Awaitable for HTTP

Library-provided awaitables for HTTP requests, timers, file I/O. Compose them like synchronous calls.

Task<std::string> fetch() {
    auto data = co_await http_get("/api/data");
    co_return data;
}

Composing Tasks

Awaiting one task inside another chains them. The outer task suspends until the inner finishes.

Task<int> outer() {
    int x = co_await inner_a();
    int y = co_await inner_b();
    co_return x + y;
}

Parallel Tasks

For parallel async work, start tasks without awaiting each one immediately, then await them all.

Task<int> sum_tasks() {
    auto a = inner_a();
    auto b = inner_b();
    int x = co_await a;
    int y = co_await b;
    co_return x + y;
}

Exception Propagation

Exceptions thrown inside an async task can be captured in the promise s unhandled_exception and rethrown on the awaiter side.

Cancellation

The standard library does not provide a cancellation primitive yet. Libraries (cppcoro, boost.cobalt) build their own — usually via cancellation tokens or stop sources.

Boost.Asio Integration

Asio supports coroutines natively. co_await on async_read, async_write, and similar operations. The cleanest C++ async I/O today.

Performance

Coroutine resumption is much cheaper than thread switching. The compiler often inlines parts of the state machine. Profile to verify your stack of awaitables compiles cleanly.

Future Directions

C++23 added std::generator and improvements. C++26 may bring std::execution (sender/receiver) — a more general async framework that interoperates with coroutines.

Quick Check

Which three methods does an awaitable type need?

Recap

Async tasks combine co_await with awaitables to write callback-free async code. Awaitables have await_ready, await_suspend, and await_resume. Libraries (cppcoro, Asio) provide ready-made task and awaitable types.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Async Tasks and Awaiter Types” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Async Tasks and Awaiter Types”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Async Tasks and Awaiter Types”?

Compose async tasks with custom awaiter and promise types. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

How long does the “Async Tasks and Awaiter Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Coroutine Concepts co_await co_yield co_return
  2. Implementing a Simple Generator
  3. Async Tasks and Awaiter Types
  4. Coroutine Frame Allocation
← Back to C++ Academy