Skip to content

Instantly share code, notes, and snippets.

@behitek
Last active August 31, 2021 11:23
Show Gist options
  • Save behitek/ea77b932859b6e7227a6e0470aca6aa5 to your computer and use it in GitHub Desktop.
Save behitek/ea77b932859b6e7227a6e0470aca6aa5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <ctime>
void timer_task(int interval, bool &worker_idle){
std::thread t([=]() {
while(1) {
if (worker_idle == true) std::cout << "timer nha ... " << &worker_idle << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
});
t.detach();
}
std::string getCurrentDateTime(){
time_t rawtime;
struct tm * timeinfo;
char buffer[80];
time (&rawtime);
timeinfo = localtime(&rawtime);
strftime(buffer,sizeof(buffer),"%Y-%m-%d %H:%M:%S",timeinfo);
std::string datetime(buffer);
return datetime;
}
int main(){
int interval = 1000;
bool worker_idle = true;
timer_task(interval, worker_idle);
// main thread continue run
int count = 0;
while(1) {
count++;
if (count == 5){
worker_idle = false;
}
std::cout << "worker nha ... " << &worker_idle << "\n";
std::this_thread::sleep_for(std::chrono::milliseconds(interval));
}
}
lap60313@LAP60313:~/sources/cpp/kaldimodeloffline/kaldikiki/src/cudadecoderbin$ g++ -pthread test.cpp
lap60313@LAP60313:~/sources/cpp/kaldimodeloffline/kaldikiki/src/cudadecoderbin$ ./a.out
worker nha ... 0x7ffcead80737timer nha ... 0x55cb0e042e78
timer nha ... 0x55cb0e042e78
worker nha ... 0x7ffcead80737
timer nha ... 0x55cb0e042e78
worker nha ... 0x7ffcead80737
timer nha ... 0x55cb0e042e78
worker nha ... 0x7ffcead80737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment