std-future

Why std::future is different returned from std::packaged_task and std::async?

早过忘川 提交于 2020-12-05 07:01:30
问题 I got to know the reason that future returned from std::async has some special shared state through which wait on returned future happened in the destructor of future. But when we use std::pakaged_task , its future does not exhibit the same behavior. To complete a packaged task, you have to explicitly call get() on future object from packaged_task . Now my questions are: What could be the internal implementation of future (thinking std::async vs std::packaged_task )? Why the same behavior was

Is there a way to check if std::future state is ready in a guaranteed wait-free manner?

江枫思渺然 提交于 2020-01-24 04:45:23
问题 I know that I can check the state of the std::future the following way: my_future.wait_for(std::chrono::seconds(0)) == std::future_status::ready But according to cppreference.com std::future::wait_for may block in some cases: This function may block for longer than timeout_duration due to scheduling or resource contention delays. Is it still the case when timeout_duration is 0 ? If so, is there another way to query the state in a guaranteed wait-free manner ? 回答1: The quote from cppreference

std::future returned from std::async hangs while going out of scope

不羁岁月 提交于 2019-12-08 07:12:36
问题 I am using a combination of std::async and std::future from C++ 11 . I am using to enforce a time_out on a certain activity that I do in my code which might take time as I try connecting to server. Following is how the code is: #include <future> #include <chrono> std::size_t PotentiallyLongRunningActivity() { using namespace std::chrono_literals; std::this_thread::sleep_for(10000s); return 10; } bool DoActivity() { bool activity_done = false; auto my_future_result(std::async(std::launch: