Reputation: 866
The sr.tFuture
is initialized with 'empty' state instead of 'pending' and I found out when I remove tFuture()
from the constructor initialization list, the state of sr.tFuture
becomes pending which is correct. But I don't quite understand the reason. Is it because the value of tFuture
on the declaration part gets overwritten when tFuture()
gets invoked and cause tFuture
to not get associated with any promise?
struct SharedResource
{
SharedResource(const SharedResource&) = delete;
SharedResource& operator=(const SharedResource&) = delete;
SharedResource() :
tPromise(), tFuture()
{}
std::promise<void> tPromise;
std::future<void> tFuture = tPromise.get_future();
};
int main()
{
SharedResource sr;
}
Upvotes: 1
Views: 654
Reputation: 1290
This answer just tries to summarize the overall understanding and resolution to the problem posted in the question, sourced majorly from the comments by françois-andrieux.
To begin with, a few clarifications.
std::promise
, std::packaged_task
or std::async
.The confusing behavior that is observed in the question is that
tFuture()
.tFuture()
is removed from the constructor initializer list, it gets initialized from the default initializer as tPromise.get_future();
.The first case sets up an invalid future, the one that OP refers to as 'empty', where as the second case sets up a proper valid future.
Upvotes: 0