Advertisement
lichenran1234

C++ TastScheduler

Apr 11th, 2021
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <functional>
  2. #include <chrono>
  3. #include <future>
  4. #include <queue>
  5. #include <thread>
  6. #include <memory>
  7.  
  8. struct function_timer
  9. {
  10.     std::function<void()> func;
  11.     std::chrono::system_clock::time_point time;
  12.  
  13.     function_timer()
  14.     { }
  15.  
  16.     function_timer(std::function<void()>&& f, std::chrono::system_clock::time_point& t)
  17.         : func(f),
  18.           time(t)
  19.     { }
  20.  
  21.     //Note: we want our priority_queue to be ordered in terms of
  22.     //smallest time to largest, hence the > in operator<. This isn't good
  23.     //practice - it should be a separate struct -  but I've done this for brevity.
  24.     bool operator<(const function_timer& rhs) const
  25.     {
  26.         return time > rhs.time;
  27.     }
  28.  
  29.     void get()
  30.     {
  31.         func();
  32.     }
  33. };
  34.  
  35. class Scheduler
  36. {
  37. private:
  38.     std::priority_queue<function_timer> tasks;
  39.     std::unique_ptr<std::thread> thread;
  40.     bool go_on;
  41.  
  42.     Scheduler& operator=(const Scheduler& rhs) = delete;
  43.     Scheduler(const Scheduler& rhs) = delete;
  44.  
  45. public:
  46.  
  47.     Scheduler()
  48.         :go_on(true),
  49.         thread(new std::thread([this]() { ThreadLoop(); }))
  50.     { }
  51.  
  52.     ~Scheduler()
  53.     {
  54.         go_on = false;
  55.         thread->join();
  56.     }
  57.  
  58.     void ThreadLoop()
  59.     {
  60.         while(go_on)
  61.         {
  62.             auto now = std::chrono::system_clock::now();
  63.             while(!tasks.empty() && tasks.top().time <= now) {
  64.                 function_timer& f = tasks.top();
  65.                 f.get();
  66.                 tasks.pop();
  67.             }
  68.  
  69.             if(tasks.empty()) {
  70.                 std::this_thread::sleep_for(std::chrono::milliseconds(100));
  71.             } else {
  72.                 std::this_thread::sleep_for(tasks.top().time - std::chrono::system_clock::now());
  73.             }
  74.         }
  75.     }
  76.  
  77.     void ScheduleAt(std::chrono::system_clock::time_point& time, std::function<void()>&& func)
  78.     {
  79.         tasks.push(function_timer(std::move(func), time));
  80.     }
  81.  
  82.     void ScheduleEvery(std::chrono::system_clock::duration interval, std::function<void()> func)
  83.     {
  84.         std::function<void()> waitFunc = [this,interval,func]()
  85.             {
  86.                 func();
  87.                 this->ScheduleEvery(interval, func);
  88.             };
  89.         ScheduleAt(std::chrono::system_clock::now() + interval, std::move(waitFunc));
  90.     }
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement