OSC++.038 — std::async Launch Policies, Futures, and Lifetime Rules

Original anime cover of an older Black woman C++ programmer working with std::async on two monitors, bottom-left neon-green Bitcoinversus.tech watermark

OSC++.038 explains std::async, the C++ standard-library function that starts or defers callable work and returns a std::future for its result or exception. The preceding OSC++.037 packaged-task lesson required the application to schedule and invoke a task. std::async instead combines task submission and future creation, while the launch policy determines when execution occurs.

Mike Shah, “std::async” follow-up lesson, November 21, 2021. An independent C++ concurrency tutorial demonstrates background loading, launch policies, future status and retrieval. Credit: Mike Shah / YouTube.

Choose between async and deferred execution

The C++ working draft specifies two standard launch flags. std::launch::async executes a callable asynchronously, as if on a separate thread, and stores its result in shared state. std::launch::deferred postpones invocation until a non-timed waiting function or get() first requests the result. A deferred task might never run if the future is abandoned without a qualifying wait.

Omitting an explicit policy effectively permits either mode. For work that must overlap with another computation, state std::launch::async explicitly. For lazy computation, choose std::launch::deferred. Cppreference documents the policy selection and explains why a default-policy call does not promise concurrent execution. OSC++.033 on callable invocation provides background on how function objects and lambdas are called.

Authentic photograph of a laptop showing source code on a desk with another computer
Authentic programming workstation photo by Negative Space, CC0 1.0, via Wikimedia Commons. The visible code is unrelated to the locally verified std::async example.

Compile an example and inspect the results

The example uses two explicit policies and prints the deferred status before calling get(). Compile using g++ -std=c++20 -Wall -Wextra -Werror -pthread main.cpp on a supported GCC installation. The code was executed locally with GCC 14.2; it printed a deferred-status line followed by 42 and 5. Timing and thread identifiers are deliberately omitted because their presentation can vary by platform.

#include <chrono>
#include <future>
#include <iostream>

int main() {
    auto work = std::async(std::launch::async, [] { return 6 * 7; });
    auto deferred = std::async(std::launch::deferred, [] { return 2 + 3; });

    if (deferred.wait_for(std::chrono::seconds(0)) == std::future_status::deferred)
        std::cout << "deferred work awaits a wait/get call\n";

    std::cout << "async result: " << work.get() << '\n';
    std::cout << "deferred result: " << deferred.get() << '\n';
}

Calling get() waits for completion when necessary, transfers the stored result, and consumes the future’s shared state. A second get() on the same std::future is invalid. If the callable throws, its exception is stored and rethrown by get(). The older OSC++.036 move-only callback lesson and OSC++.035 argument-binding lesson explain adjacent ownership and callable-design concepts.

Future lifetime and performance limits

A future returned by std::async under asynchronous execution can block when its final reference is destroyed before the task completes. Discarding the temporary future therefore may accidentally serialize supposedly concurrent tasks. Deferred work has different behavior and does not automatically launch a background thread. Microsoft’s library documentation also notes implementation-specific thread-pool behavior, making portable latency assumptions hazardous.

CppCon, “Dynamic Asynchronous Tasking with Dependencies,” presented by Tsung-Wei Huang at CppCon 2025 and published April 1, 2026. The conference talk explains limitations of basic std::async for dependency-heavy workloads and discusses a more advanced tasking library. Credit: CppCon / YouTube.

Use explicit futures, bounded workload design and documented error handling. std::async is not a cancellation API or a universal substitute for queues and task schedulers. OSC++.034 offers another way to prepare callable arguments before submission. For production systems, benchmark under representative contention and establish a shutdown strategy.

Conclusion: Explicit launch policies and careful future ownership make asynchronous behavior easier to reason about. Default selection and ignored futures can produce surprising execution timing.

Editor’s Note: The reference model was checked against the C++ working draft, cppreference and Microsoft Learn. The code block compiled and ran using GCC 14.2 under C++20; no production workload was benchmarked. The original anime-style 1200 × 630 cover includes a neon-green Bitcoinversus.tech watermark, while the in-story photograph is authentic CC0 material. Both videos are sourced from public publisher listings; actual browser playback requires independent verification.

Original BitcoinVersus.Tech QR code for an optional 5,000-satoshi Bitcoin donation
Original BitcoinVersus.Tech 5,000-satoshi donation QR. Verify the recipient independently.

BitcoinVersus.Tech is volunteer-run and publishes independent technical education. An optional 5,000-satoshi donation supports research. Scan the original QR from another screen with a compatible Bitcoin wallet; on the same phone, use a wallet capable of importing a saved QR image. Confirm recipient 3C9o19EH5HSiwEPyCTmEKzxhNCbo2X6TTb and amount independently. Bitcoin transactions are generally irreversible.

We volunteer daily to ensure the information on BitcoinVersus.Tech is Verifiably True. Coverage is educational and informational, not authorization to alter production systems, certified engineering advice, financial advice or investment advice.

Leave a Reply