2012-11-26 3 views
0

내가 내가 내가 while 1 초마다 새로 고침 원하는 C++ 통화 기능은 5 분마다

AutoFunction(){ 
    cout << "Auto Notice" << endl; 
    Sleep(60000*5); 
} 

while(1){ 

    if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){ 
     CallStart(); 
    } 

    AutoFunction(); 
    Sleep(1000); 
} 

을 시도 5 분마다 기능을
를 호출하고 싶었 동시에 call AutoFunction()에; 5 분마다,하지만 난처럼 할 생각

다른 함수를 시작하는 데 시간을 확인하는 동안 (1) 1 초를 새로해야하기 때문에 AutoFunction

Sleep을 기다리지 않고

while(1){ 

    if(current->tm_hour == StartHour && current->tm_min == StartMinut && current->tm_sec == StartSec){ 
     CallStart(); 
    } 

    Sleep(1000); 
} 
while(1){ 

    AutoFunction(); 
    Sleep(60000*5); 
} 

하지만 난 그렇게 모두

함께 일하는 것이라고 생각하지 않습니다는 THR에 익숙하지 않은 사람들을 위해 당신에게

+0

스레딩을 사용하기에 좋은 소리입니다. Thread A는 영원히 반복되며,'AutoFunction'을 호출하고 5 분 동안자는 것입니다. 동시에, 스레드 B는 영원히 반복되며 적절할 때'CallStart'를 호출하고 잠시 기다립니다. – Kevin

+0

[Boost.Asio] (http://www.boost.org/doc/libs/1_52_0/doc/html/boost_asio.html1)의 타이머 중 하나에 대한 완벽한 사용 예처럼 보입니다. [1] : –

답변

1

감사 EADS와 부스트 라이브러리는이 단일 동안 루프를 수행 할 수 있습니다

이 코드에서
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); 
} 

, curTime는 int로 유닉스 타임 스탬프를 반환 내가 만들어 기능입니다. 선택한 시간 라이브러리에서 적절한 것을 대체하십시오.

관련 문제