Why std::future is different returned from std::packaged_task and std::async?
std::async
has definite knowledge of how and where the task it is given is executed. That is its job: to execute the task. To do that, it has to actually put it somewhere. That somewhere could be a thread pool, a newly created thread, or in a place to be executed by whomever destroys the future
.
Because async
knows how the function will be executed, it has 100% of the information it needs to build a mechanism that can communicate when that potentially asynchronous execution has concluded, as well as to ensure that if you destroy the future
, then whatever mechanism that's going to execute that function will eventually get around to actually executing it. After all, it knows what that mechanism is.
But packaged_task
doesn't. All packaged_task
does is store a callable object which can be called with the given arguments, create a promise
with the type of the function's return value, and provide a means to both get a future
and to execute the function that generates the value.
When and where the task actually gets executed is none of packaged_task
's business. Without that knowledge, the synchronization needed to make future
's destructor synchronize with the task simply can't be built.
Let's say you want to execute the task on a freshly-created thread. OK, so to synchronize its execution with the future
's destruction, you'd need a mutex which the destructor will block on until the task thread finishes.
But what if you want to execute the task in the same thread as the caller of the future
's destructor? Well, then you can't use a mutex to synchronize that since it all on the same thread. Instead, you need to make the destructor invoke the task. That's a completely different mechanism, and it is contingent on how you plan to execute.
Because packaged_task
doesn't know how you intend to execute it, it cannot do any of that.
Note that this is not unique to packaged_task
. All future
s created from a user-created promise
object will not have the special property of async
's future
s.
So the question really ought to be why async
works this way, not why everyone else doesn't.
If you want to know that, it's because of two competing needs: async
needed to be a high-level, brain-dead simple way to get asynchronous execution (for which sychronization-on-destruction makes sense), and nobody wanted to create a new future
type that was identical to the existing one save for the behavior of its destructor. So they decided to overload how future
works, complicating its implementation and usage.
@Nicol Bolas has already answered this question quite satisfactorily. So I'll attempt to answer the question slightly from different perspective, elaborating the points already mentioned by @Nicol Bolas.
The design of related things and their goals
Consider this simple function which we want to execute, in various ways:
int add(int a, int b) {
std::cout << "adding: " << a << ", "<< b << std::endl;
return a + b;
}
Forget std::packaged_task
, std ::future
and std::async
for a while, let's take one step back and revisit how std::function
works and what problem it causes.
case 1 — std::function
isn't good enough for executing things in different threads
std::function<int(int,int)> f { add };
Once we have f
, we can execute it, in the same thread, like:
int result = f(1, 2); //note we can get the result here
Or, in a different thread, like this:
std::thread t { std::move(f), 3, 4 };
t.join();
If we see carefully, we realize that executing f
in a different thread creates a new problem: how do we get the result of the function? Executing f
in the same thread does not have that problem — we get the result as returned value, but when executed it in a different thread, we don't have any way to get the result. That is exactly what is solved by std::packaged_task
.
case 2 — std::packaged_task
solves the problem which std::function
does not solve
In particular, it creates a channel between threads to send the result to the other thread. Apart from that, it is more or less same as std::function
.
std::packaged_task<int(int,int)> f { add }; // almost same as before
std::future<int> channel = f.get_future(); // get the channel
std::thread t{ std::move(f), 30, 40 }; // same as before
t.join(); // same as before
int result = channel.get(); // problem solved: get the result from the channel
Now you see how std::packaged_task
solves the problem created by std::function
. That however does not mean that std::packaged_task
has to be executed in a different thread. You can execute it in the same thread as well, just like std::function
, though you will still get the result from the channel.
std::packaged_task<int(int,int)> f { add }; // same as before
std::future<int> channel = f.get_future(); // same as before
f(10, 20); // execute it in the current thread !!
int result = channel.get(); // same as before
So fundamentally std::function
and std::packaged_task
are similar kind of thing: they simply wrap callable entity, with one difference: std::packaged_task
is multithreading-friendly, because it provides a channel through which it can pass the result to other threads. Both of them do NOT execute the wrapped callable entity by themselves. One needs to invoke them, either in the same thread, or in another thread, to execute the wrapped callable entity. So basically there are two kinds of thing in this space:
- what is executed i.e regular functions,
std::function
,std::packaged_task
, etc. - how/where is executed i.e threads, thread pools, executors, etc.
case 3: std::async
is an entirely different thing
It's a different thing because it combines what-is-executed with how/where-is-executed.
std::future<int> fut = std::async(add, 100, 200);
int result = fut.get();
Note that in this case, the future created has an associated executor, which means that the future will complete at some point as there is someone executing things behind the scene. However, in case of the future created by std::packaged_task
, there is not necessarily an executor and that future may never complete if the created task is never given to any executor.
Hope that helps you understand how things work behind the scene. See the online demo.
The difference between two kinds of std::future
Well, at this point, it becomes pretty much clear that there are two kinds of std::future
which can be created:
- One kind can be created by
std::async
. Such future has an associated executor and thus can complete. - Other kind can be created by
std::packaged_task
or things like that. Such future does not necessarily have an associated executor and thus may or may not complete.
Since, in the second case the future does not necessarily have an associated executor, its destructor is not designed for its completion/wait because it may never complete:
{
std::packaged_task<int(int,int)> f { add };
std::future<int> fut = f.get_future();
} // fut goes out of scope, but there is no point
// in waiting in its destructor, as it cannot complete
// because as `f` is not given to any executor.
Hope this answer helps you understand things from a different perspective.