admin管理员组文章数量:1391947
Calling time() just after returning from std::condition_variable::wait_until() usually returns a time just before the wait until time, the previous second. Why is this?
To be clear, here is what I did:
- Get the time t using the time(nullptr) function
- Do a call to std::condition_variable::wait_until(t + 1)
- After wait_until returns, call time(nullptr) again
The second call to time returns the same value as the first call.
#include <condition_variable>
#include <ctime>
#include <iostream>
int main() {
std::condition_variable condition_variable;
std::mutex condition_mutex;
time_t now = time(nullptr);
while (true) {
time_t next_wake_time = now + 1;
std::cv_status status = std::cv_status::no_timeout;
{
std::unique_lock<std::mutex> lock(condition_mutex);
std::chrono::time_point wait_until =
std::chrono::system_clock::from_time_t(next_wake_time);
status = condition_variable.wait_until(lock, wait_until);
}
now = time(nullptr);
std::cout << ((status == std::cv_status::timeout) ? "TO" : "NT") << " "
<< std::chrono::system_clock::now().time_since_epoch().count()
<< " " << now << " " << next_wake_time << std::endl;
}
return 0;
}
The above code will print out something like this:
TO 1741903615000207944 1741903614 1741903615
TO 1741903615000382653 1741903614 1741903615
TO 1741903615000520571 1741903614 1741903615
TO 1741903615000653469 1741903614 1741903615
TO 1741903615000788007 1741903614 1741903615
TO 1741903615000930624 1741903614 1741903615
TO 1741903615001062683 1741903615 1741903615
TO 1741903616000184409 1741903615 1741903616
TO 1741903616000364146 1741903615 1741903616
TO 1741903616000507894 1741903615 1741903616
TO 1741903616000648812 1741903615 1741903616
TO 1741903616000782872 1741903615 1741903616
TO 1741903616000942440 1741903615 1741903616
TO 1741903616001090012 1741903616 1741903616
TO 1741903617000092454 1741903616 1741903617
TO 1741903617000257262 1741903616 1741903617
TO 1741903617000401176 1741903616 1741903617
TO 1741903617000536022 1741903616 1741903617
TO 1741903617000678723 1741903616 1741903617
TO 1741903617000816434 1741903616 1741903617
TO 1741903617000968836 1741903617 1741903617
TO 1741903618000089061 1741903617 1741903618
TO 1741903618000227388 1741903617 1741903618
TO 1741903618000349062 1741903617 1741903618
TO 1741903618000469941 1741903617 1741903618
TO 1741903618000590426 1741903617 1741903618
TO 1741903618000711297 1741903617 1741903618
TO 1741903618000832164 1741903617 1741903618
TO 1741903618001000293 1741903618 1741903618
I would expect time() to return something consistent with the value given to wait_until. Adding 10 ms to the wait_until variable does the trick.
This behaviour was seen on and ubuntu virtual machine as well as an arm embedded system.
版权声明:本文标题:c++ - Why is there inconsistent behaviour between time() and std::condition_variable::wait_until() - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744677969a2619227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论