简体   繁体   English

测量 std::future 对象可用之前的时间

[英]Measure time until an std::future object becomes available

I am trying to measure the duration of an API call which returns an std::future object.我正在尝试测量返回std::future对象的 API 调用的持续时间。

My current approach looks like this:我目前的方法是这样的:

std::chrono::high_resolution_clock::time_point endTime, startTime = std::chrono::high_resolution_clock::now();

std::shared_future<API::response> responseFuture = API::request();

std::future_status status = responseFuture.wait_for(3s); // Some Timeout

if (status == std::future_status::ready)
  endTime = std::chrono::high_resolution_clock::now();
else
  // error handling

However, I'm unsure about the use of std::future::wait_for .但是,我不确定std::future::wait_for cppreference.com states: cppreference.com指出:

This function may block for longer than timeout_duration due to scheduling or resource contention delays.由于调度或资源争用延迟,此函数可能会阻塞比 timeout_duration 更长的时间。

I am not worried about blocking longer than timeout_duration, but I do require wait_for to immediately return once the std::future object is available, ie not having an implementation along the lines of我不担心阻塞时间长于 timeout_duration,但我确实需要wait_forstd::future对象可用后立即返回,即没有实现

while(!ready){
  std::this_thread::sleep_for(10ms);
}

Is this guaranteed?这是有保证的吗?

The function will return once the shared state is ready, or the timeout (plus a bit) elapses.一旦共享状态准备好,或者超时(加上一点)过去,该函数将返回。 Per [futures.unique.future]/21 the effect of the function is根据 [futures.unique.future]/21 函数的效果是

None if the shared state contains a deferred function ([futures.async]), otherwise blocks until the shared state is ready or until the relative timeout ([thread.req.timing]) specified by rel_time has expired.如果共享状态包含延迟函数 ([futures.async]),则为 None,否则阻塞直到共享状态准备好或直到 rel_time 指定的相对超时 ([thread.req.timing]) 到期。

emphasis mine强调我的

And [thread.req.timing] has[thread.req.timing]

  1. Implementations necessarily have some delay in returning from a timeout.实现在从超时返回时必然有一些延迟。 Any overhead in interrupt response, function return, and scheduling induces a “quality of implementation” delay, expressed as duration Di .中断响应、函数返回和调度中的任何开销都会导致“实现质量”延迟,表示为持续时间Di Ideally, this delay would be zero.理想情况下,该延迟为零。 Further, any contention for processor and memory resources induces a “quality of management” delay, expressed as duration Dm .此外,处理器和内存资源的任何争用都会导致“管理质量”延迟,表示为持续时间Dm The delay durations may vary from timeout to timeout, but in all cases shorter is better.延迟持续时间可能因超时而异,但在所有情况下,越短越好。

  2. The functions whose names end in _for take an argument that specifies a duration.名称以 _for 结尾的函数采用指定持续时间的参数。 These functions produce relative timeouts.这些函数产生相对超时。 Implementations should use a steady clock to measure time for these functions.326 Given a duration argument Dt , the real-time duration of the timeout is Dt+Di+Dm .实现应该使用稳定的时钟来测量这些函数的时间Dt给定持续时间参数Dt ,超时的实时持续时间是Dt+Di+Dm

Which talks about the extra added time there may be added to the timeout before the function actually returns.其中谈到在函数实际返回之前可能会增加超时的额外时间。

You are working in a preemptive multithreading environment;您正在抢占式多线程环境中工作; nothing happens "immediately".没有什么“立即”发生。 Once you give your timeslice over to the OS by blocking (or it steals your timeslice), you've given up control.一旦您通过阻塞(或它窃取您的时间片)将您的时间片交给操作系统,您就已经放弃了控制权。 The OS will unblock your thread at some point after the mutex is unlocked.互斥锁解锁后,操作系统将在某个时候解除对线程的阻塞。 OS's try to be prompt about these things, but there are no guarantees.操作系统试图对这些事情做出提示,但不能保证。

The best you can hope for is "soon after".您所能希望的最好结果是“不久之后”。 wait_for will get you that. wait_for会让你知道。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM