获取std :: future的状态 [英] Get the status of a std::future

查看:994
本文介绍了获取std :: future的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查 std :: future 是否已完成?就我可以告诉的唯一方法是调用 wait_for 与零持续时间,并检查状态是否准备

Is it possible to check if a std::future has finished or not? As far as I can tell the only way to do it would be to call wait_for with a zero duration and check if the status is ready or not, but is there a better way?

推荐答案

你是正确的,除了调用 wait_until 与过去的时间(这是等价的)没有更好的方法。

You are correct, and apart from calling wait_until with a time in the past (which is equivalent) there is no better way.

你可以总是写一个小包装如果你想要一个更方便的语法:

You could always write a little wrapper if you want a more convenient syntax:

template<typename R>
  bool is_ready(std::future<R> const& f)
  { return f.wait_for(std::chrono::seconds(0)) == std::future_status::ready; }

N.B。如果函数被deferred,这将永远不会返回true,所以可能最好直接检查 wait_for 在你可能要在一段时间后同步运行延迟任务的情况下通过或系统负载低时。

N.B. if the function is deferred this will never return true, so it's probably better to check wait_for directly in the case where you might want to run the deferred task synchronously after a certain time has passed or when system load is low.

这篇关于获取std :: future的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆