0Pricing
C++ Academy · Lesson

std::async and std::future Basics

Run tasks asynchronously and retrieve their results via std::future.

std::async and std::future Basics is a free C++ Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Asynchronous Tasks

std::async runs a function asynchronously and returns a std::future that delivers the result. A higher-level alternative to manually managing threads.

Basic Usage

Pass a callable and arguments. std::async returns a std::future<Result>.

#include <future>

int compute() { return 42; }

std::future<int> f = std::async(compute);
int result = f.get();   // blocks until done

Launch Policies

The first argument controls when the task runs:

  • std::launch::async — start a new thread immediately
  • std::launch::deferred — run synchronously on get()
  • Default (no policy) — implementation chooses
auto f = std::async(std::launch::async, compute);

std::future Methods

Common operations:

  • get() — blocks until done and returns the result (once only)
  • wait() — blocks but does not return the result
  • wait_for(d) / wait_until(t) — timed waits
  • valid() — has this future been set up?

Exception Propagation

If the async task throws, the exception is stored in the future and re-thrown on get().

auto f = std::async([] { throw std::runtime_error("oops"); });
try {
    f.get();
} catch (const std::exception& e) {
    std::cout << e.what();
}

Returning Multiple Values

Wrap the result in a tuple, struct, or container.

auto f = std::async([] {
    return std::make_pair(42, std::string("hello"));
});
auto [n, s] = f.get();

Multiple Tasks in Parallel

Launch several async tasks and wait for all of them.

auto a = std::async(work_a);
auto b = std::async(work_b);
auto c = std::async(work_c);

std::cout << a.get() + b.get() + c.get();

std::promise: Manual Future

For producer/consumer patterns where the consumer needs a future but the producer is in another thread, use std::promise.

std::promise<int> p;
std::future<int> f = p.get_future();

std::thread t([&p] { p.set_value(42); });
std::cout << f.get();
t.join();

std::packaged_task

Bundles a callable with a future. Useful when the task is invoked elsewhere (thread pool).

shared_future

A std::future can only be read once. std::shared_future allows multiple readers — useful for broadcasting a result.

Destructor Behavior

The destructor of a future returned by std::async with the default policy may block until the task finishes. Be aware in long-running tasks.

Beyond std::async

For production-grade async, consider libraries like Boost.Asio, Folly, or the C++20/23 executors and coroutines. std::async is a starting point.

Quick Check

What does calling .get() on a std::future do?

Recap

std::async runs a function asynchronously; the returned std::future delivers the result and propagates exceptions. Use launch policies to control execution. For complex patterns, use std::promise and std::packaged_task.

Frequently asked questions

Is the “std::async and std::future Basics” lesson free?

Yes — the full text of “std::async and std::future Basics” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “std::async and std::future Basics”?

Run tasks asynchronously and retrieve their results via std::future. 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; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “std::async and std::future Basics” 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. std::thread Joining and Detaching
  2. std::mutex lock_guard and unique_lock
  3. Condition Variables and Worker Patterns
  4. std::async and std::future Basics
← Back to C++ Academy