当sleep_until()指定过去的某个时间点时,行为是否定义明确? [英] Is behaviour well-defined when `sleep_until()` specifies a time point in the past?

查看:344
本文介绍了当sleep_until()指定过去的某个时间点时,行为是否定义明确?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++ 11标准讨论了如果调整系统时钟以使传递到sleep_until()的时间点现在已经过去了,那该怎么办?但是当指定了时,我看不到任何能解决这种情况的地方时间点已经过去了.

The C++11 standard talks about what should happen if the system clock is adjusted such that the time point passed to sleep_until() is now in the past - but I can't see anywhere that addresses the case when the specified time point is already in the past.

我只是忽略了某些东西,还是真的没有指定-甚至是UB或实现定义的东西?

Have I simply overlooked something, or is it really not specified - even as UB or implementation-defined?

如果在持续时间为负的情况下调用sleep_for(),也会出现类似的问题.

A similar question arises if sleep_for() is invoked with a negative duration.

推荐答案

计算直到进入睡眠状态并调用sleep_until()所需的时间不是原子的.您可能会计算时间,然后发生上下文切换,系统过载,交换,并且对sleep_until()的实际调用发生的时间要晚得多.因此,如果sleep_until()在过去的时间里没有唤醒,则它是无用的,因为在这种情况下,您将无法确定线程将被唤醒.

Calculation of time until which to sleep and calling sleep_until() is not atomic. It is possible that you calculate time, then context switch occurs, system is overloaded, swapping, and actual call to sleep_until() happens much later. So if sleep_until() does not wake up when time is in the past, then it is useless, because in such situation you never can be sure that your thread will be waken.

该功能的要求在标准的30.2.4节中指定.它指定返回时间应为Ct + Di + Dm,其中Ct是您指定的时间,Di是由开销中断,函数返回和调度引起的延迟,而Dm是由资源争用引起的延迟.在这种情况下,Di包含您调用sleep_until()之前经过的时间,该函数将尽快返回.

Requirements for the function are specified in section 30.2.4 of the standard. And it specifies that return time should be Ct + Di + Dm where Ct is the time you have specified, Di is a delay induced by overhead oof interrupt, function return and scheduling, and Dm is delay caused by resource contention. In such case Di includes time that passed before you have called sleep_until() and the function returns as soon as it can.

这篇关于当sleep_until()指定过去的某个时间点时,行为是否定义明确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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