My code:
void job_function(std::promise<void>& p) { do_it(); p.set_value(); } void foo() { std::promise<void> p; auto* thread = new std::thread(job_function, p); p.get_future().wait_for(std::chrono::seconds(1)); } In this code, if the calling thread of foo only waits for 1 second until the future completes. It is quite possible that the actual job gets completed after the wait is over. In this scenario, p is destructed already so call to p.set_value will not work. I can create p on heap, but even in that case it should be deleted and which thread should delete p depends on order of job completion and wait_for. Is there a specific pattern that can be used to handle this?