The problem is not that std::future::wait
not blocked. The real problem is that you have a race condition between the thread you spawned, execute it, and destroying the std::thread
(temporary) object in the main thread.
Because of this, abort
is called in the std::thread
destructor if the thread is still connecting.
Work code:
#include <iostream> #include <string> #include <thread> #include <future> #include <chrono> int main() { auto pms = std::promise<std::string>(); auto ftr = pms.get_future(); std::thread thread ([&](){pms.set_value("hello world");}); ftr.wait(); std::cout << ftr.get() << std::endl; thread.join (); return 0; }
Please note that if you do not explicitly join the thread
, you will still have the same race condition (since it is possible that main
can do its job faster than thread
can clear itself.
Demo of a working example: here .
source share