如何用c+计算时间+;? 我试图找出如何计算C++中的时间。我正在做 每3秒钟发生一次事件的程序,例如打印“hello”等
您可以使用如何用c+计算时间+;? 我试图找出如何计算C++中的时间。我正在做 每3秒钟发生一次事件的程序,例如打印“hello”等,c++,time.h,C++,Time.h,您可以使用boost::timer在C++中计算时间: using boost::timer::cpu_timer; using boost::timer::cpu_times; using boost::timer::nanosecond_type; ... nanosecond_type const three_seconds(3 * 1000000000LL); cpu_timer timer; cpu_times const elapsed_times(timer.elapse
boost::timer
在C++中计算时间:
using boost::timer::cpu_timer;
using boost::timer::cpu_times;
using boost::timer::nanosecond_type;
...
nanosecond_type const three_seconds(3 * 1000000000LL);
cpu_timer timer;
cpu_times const elapsed_times(timer.elapsed());
nanosecond_type const elapsed(elapsed_times.system + elapsed_times.user);
if (elapsed >= three_seconds)
{
//more then 3 seconds elapsed
}
您可以使用
boost::timer
在C++中计算时间:
using boost::timer::cpu_timer;
using boost::timer::cpu_times;
using boost::timer::nanosecond_type;
...
nanosecond_type const three_seconds(3 * 1000000000LL);
cpu_timer timer;
cpu_times const elapsed_times(timer.elapsed());
nanosecond_type const elapsed(elapsed_times.system + elapsed_times.user);
if (elapsed >= three_seconds)
{
//more then 3 seconds elapsed
}
下面是一个使用两个线程的示例,这样您的程序就不会冻结,并在C++11中使用
this\u thread::sleep\u for()
:
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void hello()
{
while(1)
{
cout << "Hello" << endl;
chrono::milliseconds duration( 3000 );
this_thread::sleep_for( duration );
}
}
int main()
{
//start the hello thread
thread help1(hello);
//do other stuff in the main thread
for(int i=0; i <10; i++)
{
cout << "Hello2" << endl;
chrono::milliseconds duration( 3000 );
this_thread::sleep_for( duration );
}
//wait for the other thread to finish in this case wait forever(while(1))
help1.join();
}
#包括
#包括
#包括
使用名称空间std;
void hello()
{
而(1)
{
cout下面是一个使用两个线程的示例,这样您的程序就不会冻结,并且在C++11中,这个线程::sleep\u for()
#include <iostream>
#include <chrono>
#include <thread>
using namespace std;
void hello()
{
while(1)
{
cout << "Hello" << endl;
chrono::milliseconds duration( 3000 );
this_thread::sleep_for( duration );
}
}
int main()
{
//start the hello thread
thread help1(hello);
//do other stuff in the main thread
for(int i=0; i <10; i++)
{
cout << "Hello2" << endl;
chrono::milliseconds duration( 3000 );
this_thread::sleep_for( duration );
}
//wait for the other thread to finish in this case wait forever(while(1))
help1.join();
}
#包括
#包括
#包括
使用名称空间std;
void hello()
{
而(1)
{
cout这取决于您的操作系统/编译器
案例1:
如果您有C++11,那么您可以按照Chris的建议使用:
std::this_thread::sleep_for()//必须包含头文件线程
案例二:
如果您使用的是windows平台,则还可以使用以下内容:
#include windows.h
int main ()
{
event 1;
Sleep(1000); // number is in milliseconds 1Sec = 1000 MiliSeconds
event 2;
return 0;
}
案例3:
在linux平台上,您只需使用:
睡眠(秒);它取决于您的操作系统/编译器
案例1:
如果您有C++11,那么您可以按照Chris的建议使用:
std::this_thread::sleep_for()//必须包含头文件线程
案例二:
如果您使用的是windows平台,则还可以使用以下内容:
#include windows.h
int main ()
{
event 1;
Sleep(1000); // number is in milliseconds 1Sec = 1000 MiliSeconds
event 2;
return 0;
}
案例3:
在linux平台上,您只需使用:
睡眠(秒)C++本身并不提供任何事件系统,但在任何情况下,我都不知道系统时间和日期有助于设置一个循环事件。你总是可以创建一个线程,它接收一个函数并循环,睡三秒并调用它。重用。如果我要这样做,那么也要花时间睡觉。你不仅仅是在寻找一个函数吗?启动一个新线程,让它在你睡觉的地方无休止地循环3秒钟,然后打印hello。@A4L,或std::this_thread::sleep_for
,这是标准的C++11。我想使用difftime,但睡眠会更好,对吗?它是在C++中,C++本身并不提供任何事件系统,但在任何情况下,我都不知道系统时间和日期有助于设置循环事件。你总是可以创建一个线程,该线程接受一个函数并循环,睡三秒并调用它。不过,要做到这一点,也要花时间睡觉。你不仅仅是在寻找一个函数吗?启动一个新线程,让它在你睡觉的地方无休止地循环3秒钟,然后打印hello。@A4L,或std::this_thread::sleep_for
,这是标准的C++11。我想使用difftime,但睡眠会更好,对吗?