使用时钟"storing"特定时间

"storing" a specific time with clock

本文关键字:定时间 storing 时钟      更新时间:2023-10-16

所以我想知道我是否可以使用一个变量或其他东西,并保留clock存储时的值。例如:

if (this)
that = Clock();

这样我就可以做了

if (that + 20000)//20 seconds later
dothing();

非常感谢任何帮助:D

你应该研究std::chrono的东西,它有办法获得当前时间,添加持续时间,并做各种其他精彩的事情。

例如:

#include <iostream>
#include <chrono>
using namespace std::chrono;
int main() {
auto start = system_clock::now();
while (duration_cast<seconds>(system_clock::now() - start).count() < 5)
;
// It is now 5 seconds later.
}

在c++11 中使用sleep_until

#include <iostream>
#include <chrono>
#include <thread>    
int main()
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::chrono::seconds sec(2);
std::this_thread::sleep_until(now+sec);
std::cout << " done "<<std::endl;
}