mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Add external timer class
This commit is contained in:
Vendored
+12
@@ -0,0 +1,12 @@
|
||||
add_library(Timer STATIC)
|
||||
|
||||
target_sources(Timer
|
||||
PRIVATE
|
||||
${CMAKE_CURRENT_LIST_DIR}/timer.cpp
|
||||
|
||||
)
|
||||
|
||||
target_include_directories(Timer
|
||||
PUBLIC
|
||||
${CMAKE_CURRENT_LIST_DIR}
|
||||
)
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
#include "timer.hpp"
|
||||
|
||||
Timer::Timer(void (*function)()) {
|
||||
this->function = function;
|
||||
}
|
||||
|
||||
void Timer::setTimeout(std::chrono::milliseconds delay) {
|
||||
|
||||
this->p_clear = false;
|
||||
std::thread t([this, delay]() {
|
||||
if(this->p_clear) return;
|
||||
std::this_thread::sleep_for(delay);
|
||||
if(this->p_clear) return;
|
||||
function();
|
||||
});
|
||||
t.detach();
|
||||
}
|
||||
|
||||
void Timer::setInterval(std::chrono::milliseconds interval) {
|
||||
this->p_clear = false;
|
||||
std::thread t([this, interval]() {
|
||||
while(true) {
|
||||
if(this->p_clear) return;
|
||||
std::this_thread::sleep_for(interval);
|
||||
if(this->p_clear) return;
|
||||
function();
|
||||
}
|
||||
});
|
||||
t.detach();
|
||||
}
|
||||
|
||||
void Timer::stop() {
|
||||
this->p_clear = true;
|
||||
}
|
||||
Vendored
+15
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
class Timer {
|
||||
public:
|
||||
Timer(void (*function)());
|
||||
void setTimeout(std::chrono::milliseconds delay);
|
||||
void setInterval(std::chrono::milliseconds interval);
|
||||
void stop();
|
||||
private:
|
||||
bool p_clear = false;
|
||||
std::mutex p_clear_mutex;
|
||||
void (*function)();
|
||||
};
|
||||
Reference in New Issue
Block a user