The C++11 standard does not provide a direct way to cancel a task started with std::async
. You will have to implement your own cancellation mechanism, such as passing in an atomic flag variable to the async task which is periodically checked.
Your code should not crash though. On reaching the end of main
, the std::future<int>
object held in result
is destroyed, which will wait for the task to finish, and then discard the result, cleaning up any resources used.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…