C++ c++;每5分钟调用一次函数
我想每5分钟调用一次函数C++ c++;每5分钟调用一次函数,c++,while-loop,C++,While Loop,我想每5分钟调用一次函数 我试过了 但我不认为两人会一起工作 感谢您对于我们这些不熟悉线程和Boost库的人,这可以通过一个while循环完成: void AutoFunction(){ cout << "Auto Notice" << endl; } //desired number of seconds between calls to AutoFunction int time_between_AutoFunction_calls = 5*60; int
我试过了 但我不认为两人会一起工作
感谢您对于我们这些不熟悉线程和Boost库的人,这可以通过一个while循环完成:
void AutoFunction(){
cout << "Auto Notice" << endl;
}
//desired number of seconds between calls to AutoFunction
int time_between_AutoFunction_calls = 5*60;
int time_of_last_AutoFunction_call = curTime() - time_between_AutoFunction_calls;
while(1){
if (should_call_CallStart){
CallStart();
}
//has enough time elapsed that we should call AutoFunction?
if (curTime() - time_of_last_AutoFunction_call >= time_between_AutoFunction_calls){
time_of_last_AutoFunction_call = curTime();
AutoFunction();
}
Sleep(1000);
}
void AutoFunction(){
听起来是使用线程的好时机。线程a永远循环,调用AutoFunction
并休眠五分钟。同时,线程B永远循环,在适当的时候调用CallStart
并休眠一秒钟。看起来像是一个完美的使用案例,适用于一个计时器。[1]:
while(1){
if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){
CallStart();
}
Sleep(1000);
}
while(1){
AutoFunction();
Sleep(60000*5);
}
void AutoFunction(){
cout << "Auto Notice" << endl;
}
//desired number of seconds between calls to AutoFunction
int time_between_AutoFunction_calls = 5*60;
int time_of_last_AutoFunction_call = curTime() - time_between_AutoFunction_calls;
while(1){
if (should_call_CallStart){
CallStart();
}
//has enough time elapsed that we should call AutoFunction?
if (curTime() - time_of_last_AutoFunction_call >= time_between_AutoFunction_calls){
time_of_last_AutoFunction_call = curTime();
AutoFunction();
}
Sleep(1000);
}