The problem that a promise / future exists is to pop a value from one thread to another. It may also throw an exception.
Thus, the source stream must have some object with which it can talk in order to send the desired value to another stream. Well ... who owns this object? If the source has a pointer to what belongs to the target stream, how does the source know if the target stream has deleted the target? Perhaps the destination stream no longer cares about cost; perhaps something has changed so that he just decided to throw his thread on the floor and forget about it.
This legal code is in some cases.
So, now the question is why the source does not own the promise and simply indicates the addressee a pointer / link to it? Well, there is a good reason for this: the promise belongs to the original thread. As soon as the source thread is completed, the promise will be destroyed. Thus, leaving the destination stream a reference to the destroyed promise.
Unfortunately.
Therefore, the only viable solution is to have two fully functional objects: one for the source and one for the destination. These objects share ownership of the transferred value. Of course, this does not mean that they cannot be of the same type; you could have something like shared_ptr<promise> or somesuch. After all, a promise / future should have some kind of shared repository of some kind inside, right?
However, consider the promise / future interface as they currently stand.
promise not copied . You can move it, but you cannot copy it. future also not copied, but future can become shared_future , which can be copied. This way you can have several destinations, but only one source.
promise can set value only; he can't even get it back. future can only get value; he cannot install it. Therefore, you have an asymmetric interface that is fully suitable for this use case. You do not want the destination to be able to set the value and source to get it. This is the reverse logic of the code.
So why do you need two objects. You have an asymmetric interface and is best handled by two related, but separate types and objects.
Nicol bolas
source share