A C++ thumbnail generator calls std::async(std::launch::async, make_thumbnail, img) in a loop, keeping each future only for that iteration; why does it run sequentially?
answer
- look at the end of the loop body
- the last reference to an async state
- the destructor waits for the task
- keep the futures alive
basics
~20 sEach iteration's std::future dies at the end of the loop body, and the last future of an unfinished std::async task may block in its destructor until the task ends, as libc++ and libstdc++ do. Keep the futures and get() them after the loop.
solid answer
~40 sThe standard says releasing a shared state never blocks, **except** that it may block when the state was created by `std::async`, is not yet ready, and this is the last reference. A `std::future` from `std::async(std::launch::async, ...)` destroyed at the end of each iteration is exactly that case, and libc++ and libstdc++ both wait there, so image *n+1* starts only after image *n* finishes. The fix is to keep the futures — `std::vector<std::future<void>> pending;` — and call `get()` on each after the loop, which also surfaces exceptions. Futures from `std::promise` or `std::packaged_task` never block in their destructor. C++20 and C++23 declare `std::async` `[[nodiscard]]`, so discarding the result outright draws a warning, but a named future that dies each iteration does not.
code
cpp · 31 lines#include <chrono>
#include <cstdio>
#include <future>
#include <thread>
#include <vector>
using namespace std::chrono_literals;
using Clock = std::chrono::steady_clock;
void make_thumbnail(int image) {
std::this_thread::sleep_for(100ms); // stands in for decode + resize
(void)image;
}
long ms_since(Clock::time_point t0) {
return std::chrono::duration_cast<std::chrono::milliseconds>(Clock::now() - t0).count();
}
int main() {
auto t0 = Clock::now();
for (int i = 0; i < 4; ++i) {
auto f = std::async(std::launch::async, make_thumbnail, i);
} // f is destroyed here and waits for its task
std::printf("future per iteration: %ld ms\n", ms_since(t0));
t0 = Clock::now();
std::vector<std::future<void>> pending;
for (int i = 0; i < 4; ++i)
pending.push_back(std::async(std::launch::async, make_thumbnail, i));
for (auto& f : pending) f.get();
std::printf("futures kept: %ld ms\n", ms_since(t0));
}go deeper
Remember that the future returned by std::async must be kept until you call get(); dropping it early can make the call wait.
State the rule: only the last future of a not-yet-ready std::async state may block when destroyed, and show the vector-of-futures fix.
Diagnose 'parallel code that runs serially' from future lifetimes, including unwinding and early returns, and know which futures never block.
Treat hidden blocking in destructors as a reason to route background work through an owned pool or task system rather than ad-hoc std::async calls.
## The symptom A tool generates thumbnails for a folder of images. Each call to `make_thumbnail` takes about 100 ms, and the author launches them with `std::async(std::launch::async, make_thumbnail, img)` so they run in parallel. On four images the run takes 400 ms instead of 100 ms. Profiling shows one worker thread busy at a time. Nothing in the code calls `get()` or `wait()` inside the loop, so where is the waiting? ## What the standard says The draft defines what it means for a future to **release** its shared state: if it holds the last reference the state is destroyed, the reference is given up, and > these actions will not block for the shared state to become ready, except that it may block if all of the following are true: the shared state was created by a call to `std::async`, the shared state is not yet ready, and this was the last reference to the shared state. `~future()` releases the shared state. The `std::async` specification repeats the point in a note: "If a future obtained from `async` is moved outside the local scope, the future's destructor can block for the shared state to become ready." So a future that goes out of scope at the end of a loop body, while its task is still running, may wait for the task to finish. The standard says "may"; the two libraries checked here both do: - **libstdc++** (GCC 16): the async state's destructor joins the worker thread. - **libc++** (LLVM 23): releasing the last reference calls `wait()` on the state before destroying it. ## Which futures block in their destructor | Where the future came from | Can its destructor wait? | |---|---| | `std::async(std::launch::async, ...)`, task still running, last reference | **yes** | | `std::async(std::launch::deferred, ...)` | no; the deferred function simply never runs | | `std::promise::get_future()` | no | | `std::packaged_task::get_future()` | no | | A `std::shared_future` copy that is not the last reference | no | This is why the behaviour surprises people: it applies only to the `std::async` flavour of an otherwise uniform type, and nothing in the type tells you which flavour you hold. ## The fix - Keep every future alive for as long as the work should overlap: `std::vector<std::future<void>> pending; pending.push_back(std::async(std::launch::async, make_thumbnail, img));`. - After launching all of them, call `get()` on each. That is where the waiting belongs, and it also rethrows any exception a task stored. - If results should be consumed as they finish rather than in launch order, poll with `wait_for(0s)` or move to a work queue; the standard `std::future` has no "wait for any". ## What the compiler can tell you C++20 and C++23 declare both `std::async` overloads `[[nodiscard]]`, so `std::async(std::launch::async, task);` as an expression statement — a temporary future destroyed at the semicolon, which blocks at once — draws a warning; clang reports "ignoring return value of function declared with 'nodiscard' attribute". C++26 removed `[[nodiscard]]` from the library specification and left such markings to implementations; libc++ and libstdc++ still mark `std::async`. No warning helps with the named-future-per-iteration form: the result *is* used, it just does not live long enough. ## What the waiting buys The blocking destructor is a deliberate trade-off, and it is easier to accept once its effect is clear. A `std::async` task under `launch::async` behaves as if run by a `std::thread`, and the specification says waiting on its future behaves as if the thread were joined. If the last future could be dropped without waiting, the task would carry on like a detached thread, possibly reading local variables of a function that has already returned, and nothing would ever join it. Waiting in the destructor keeps the task's lifetime nested inside the lifetime of its future. The price is the serialisation in this question whenever the future's lifetime is shorter than the author intended. ## The same mechanism elsewhere - **Exceptions.** If code launches several tasks into local futures and then throws, stack unwinding destroys the futures and waits for every running task before the exception reaches its handler. - **Early returns.** A function that returns while an `std::async` future is still a local waits on the way out, which can turn a "fire and forget" helper into a synchronous call. - **Fire and forget is not available.** There is no way to detach a `std::future`; if a task truly must outlive its launcher, a `std::thread` or a long-lived pool owns it instead.
- Does a future from std::promise or std::packaged_task also block in its destructor?No. The exception in the standard's release rule applies only to a shared state created by `std::async` that is not yet ready and whose last reference is being dropped. A future obtained from `promise::get_future()` or `packaged_task::get_future()` just gives up its reference; if the producer later sets a value, nobody reads it.
- Why does [[nodiscard]] not catch the loop version of this bug?`[[nodiscard]]` only warns when the return value is discarded. Writing `auto f = std::async(...);` uses the value by initializing `f`, so there is no warning, yet `f` is destroyed at the end of the loop body while the task still runs. The defect is the future's lifetime, which the attribute cannot see.
- Is the blocking guaranteed by the standard?No: the wording says releasing the last reference to an unready `std::async` state *may* block. libc++ and libstdc++ do block, which is what nearly all code relies on to avoid a task outliving its data. Portable code should not depend on either behaviour; keeping futures alive and calling `get()` is correct under both.
- Every std::future destructor blocks until its result is ready
- launch::async guarantees the loop runs in parallel whatever happens to the future
- [[nodiscard]] on std::async catches every case of this bug
- Calling detach() on the future lets the task keep running
- Switching to launch::deferred removes the wait and keeps the parallelism
Assumes C++20 or C++23 for the [[nodiscard]] on std::async; the blocking destructor rule itself dates from C++11, and C++26 removes [[nodiscard]] from the library wording while implementations keep it.