Can I change a std::sync(std::launch::deferred policy to std::launch::async, once created?

67 views Asked by At

I'm in a scenario where I have many I/O-bound tasks, nicely suited to run in background. However, I can't launch all of them in parallel.

Although I could use other tricks (eg. count semaphores), I wonder if I could change an already created std::async(deferred) (well, future, right now) policy to async each time one of the futures completes (well, kind of, but I hope you get some context).

Once I have a deferred future, the only way to start it is block until it ends (wait or get, not even wait_for). I'd just want to make it run, but without waiting for it to complete (right now; I will accept the block, but later).

1

There are 1 answers

0
olepinto On

Not the real thing, but this may do the trick in some scenarios:

    auto deferred_f = std::async(std::launch::deferred, some_function);

    auto def_to_async = [](auto &&f) { return f.get(); };

    auto async_f = std::async(std::launch::async, def_to_async, std::move(deferred_f));

deferred_f is now gone, though. If anybody has any reference or pointer to it, it won't work.