A more reliable implementation of 99x's timercpp.
Add timercpp.h
and timercpp.cpp
to your project.
Timer timer;
timer.setInterval([=]()
{
static int cnt = 0;
++cnt;
std::cout << "- Count " << cnt << std::endl;
}, 1000);
//Note that new task will kill previous task if it exists.
timer.setTimeout([=]()
{
std::cout << "- Timeout" << std::endl;
}, 1000);
//Kill background task, it will be automatically killed when leaving a scope.
timer.stop();
Automatically stop when leaving scope.
void scope() { Timer timer; timer.setTimeout([=]() { std::cout << "- Timeout" << std::endl; }, 1000); //Stops when leaving } int main() { scope(); //Will print nothing. ... }
Unlike 99x's timercpp, this project uses boost.thread
for interrupt feature.
You can use
vcpkg
to installboost
./vcpkg install boost
Only one task per Timer object.