How to check if a std::async task is finished?

前端 未结 2 817
生来不讨喜
生来不讨喜 2021-02-07 00:43

In my graphics application I want to generate a batch meshes in another thread. Therefore I asynchrony call the member function using std::async.

ta         


        
相关标签:
2条回答
  • 2021-02-07 01:06

    Use future::wait_for(). You can specify a timeout, and after that, get a status code.

    Example:

    task.wait_for(std::chrono::seconds(1));
    

    This will return future_status::ready, future_status::deferred or future_status::timeout, so you know the operation's status. You can also specify a timeout of 0 to have the check return immediately as soon as possible.

    0 讨论(0)
  • 2021-02-07 01:17

    There's an is_ready member function in the works for std::future. In the meantime, the VC implementation has an _Is_ready() member.

    0 讨论(0)
提交回复
热议问题