123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- #include <chrono>
- #include <functional>
- #include <list>
- #include <mutex>
- #include <thread>
- #include <vector>
- #include <iostream>
- class TimerWheel {
- public:
- using Task = std::function<void()>;
-
- explicit TimerWheel(size_t wheel_size, int interval_ms)
- : wheel_size_(wheel_size),
- interval_ms_(interval_ms),
- wheel_(wheel_size),
- current_index_(0) {}
- ~TimerWheel() {
- Stop();
- }
-
- void Start() {
- if (running_) {
- return;
- }
- running_ = true;
-
- thread_ = std::thread([this]() {
- while (running_) {
- std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms_));
- Tick();
- }
- std::cout << "timer oooops!" << std::endl;
- });
- thread_.detach();
- }
- void Stop() {
- if (!running_) {
- return;
- }
- running_ = false;
- if (thread_.joinable()) {
- thread_.join();
- }
- }
-
- void AddTask(int timeout_ms, Task task) {
- std::lock_guard<std::mutex> lock(mutex_);
-
- size_t ticks = timeout_ms / interval_ms_;
-
- size_t index = (current_index_ + ticks) % wheel_size_;
-
- size_t allindex = index;
- for (size_t i = 1 ; allindex < wheel_size_; i++)
- {
- allindex = index * i;
- if (allindex >= wheel_size_)
- break;
- wheel_[allindex].push_back(task);
- }
-
- }
- private:
-
- void Tick() {
- std::lock_guard<std::mutex> lock(mutex_);
-
- auto& tasks = wheel_[current_index_];
- for (const auto& task : tasks) {
- task();
- }
-
-
- current_index_ = (current_index_ + 1) % wheel_size_;
- }
- private:
- size_t wheel_size_;
- int interval_ms_;
- std::vector<std::list<Task>> wheel_;
- size_t current_index_;
- bool running_ = false;
- std::thread thread_;
- std::mutex mutex_;
- };
|