38 lines
710 B
C++
38 lines
710 B
C++
//
|
|
// Created by xtkuang on 2025/5/14.
|
|
//
|
|
|
|
#ifndef CMVR_ES_TIMER_H
|
|
#define CMVR_ES_TIMER_H
|
|
|
|
#include <sys/timerfd.h>
|
|
#include <unistd.h>
|
|
#include <functional>
|
|
#include <thread>
|
|
#include <atomic>
|
|
#include <system_error>
|
|
#include <iostream>
|
|
#include <cstring>
|
|
#include <utility>
|
|
|
|
class FDTimer {
|
|
public:
|
|
using Callback = std::function<void()>;
|
|
|
|
FDTimer();
|
|
~FDTimer();
|
|
void start(std::chrono::nanoseconds interval, Callback callback);
|
|
void stop();
|
|
[[nodiscard]] bool is_running() const;
|
|
|
|
private:
|
|
static timespec toTimespec(std::chrono::nanoseconds ns);
|
|
|
|
int fd_;
|
|
std::atomic<bool> running_;
|
|
Callback callback_;
|
|
std::thread worker_thread_;
|
|
};
|
|
|
|
#endif //CMVR_ES_TIMER_H
|