Advertisement
lichenran1234

C++ TastScheduler2

May 10th, 2021
553
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | None | 0 0
  1. #include <functional>
  2. #include <chrono>
  3. #include <future>
  4. #include <queue>
  5. #include <thread>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9.  
  10. struct TimerFunction {
  11.     function<void()> func;
  12.     chrono::system_clock::time_point time;
  13.    
  14.     TimerFunction() {}
  15.    
  16.     TimerFunction(function<void()> & func, chrono::system_clock::time_point & time) : func(func), time(time) {}
  17.    
  18.     bool operator < (const TimerFunction & rhs) const {
  19.         return this->time > rhs.time;
  20.     }
  21. };
  22.  
  23. class TaskScheduler {
  24.  public:
  25.     TaskScheduler(int num_threads){
  26.         for(int i = 0; i < num_threads; ++i) {
  27.             worker_threads_.emplace_back(make_unique<thread>(&TaskScheduler::FunctionRunner, this));
  28.         }
  29.         cout << "creating scheduler" << endl;
  30.     }
  31.     ~TaskScheduler() {
  32.         is_running_ = false;
  33.         for (const auto& worker : worker_threads_) {
  34.             worker->join();
  35.         }
  36.         cout << "deleting scheduler" << endl;
  37.     }
  38.    
  39.     void Schedule(function<void()> && func, chrono::system_clock::time_point && time) {
  40.         if (!is_running_) {
  41.             return;
  42.         }
  43.         unique_lock<mutex> lock(mutex_);
  44.         queue_.push(TimerFunction(func, time));
  45.         lock.unlock();
  46.         cv_.notify_one();
  47.     }
  48.  private:
  49.     void FunctionRunner() {
  50.         while (is_running_) {
  51.             auto now = chrono::system_clock::now();
  52.            
  53.             unique_lock<mutex> lock(mutex_);
  54.             cv_.wait_for(lock, 5ms, [this]() {return !queue_.empty();});
  55.            
  56.             if (queue_.empty()) {
  57.                 continue;
  58.             }
  59.            
  60.             auto timer_func = queue_.top();
  61.             if (timer_func.time > now) {
  62.                 continue;
  63.             }
  64.             queue_.pop();
  65.             lock.unlock();
  66.            
  67.             timer_func.func();
  68.         }
  69.     }
  70.    
  71.     condition_variable cv_;
  72.     mutex mutex_;
  73.     priority_queue<TimerFunction> queue_;
  74.     bool is_running_ = true;
  75.     vector<unique_ptr<thread>> worker_threads_;
  76. };
  77.  
  78. int main(int argc, char** argv) {
  79.     TaskScheduler scheduler(5);
  80.     scheduler.Schedule([]() {cout << "50ms" << endl;}, chrono::system_clock::now() + chrono::milliseconds(50));
  81.     scheduler.Schedule([]() {cout << "10ms" << endl;}, chrono::system_clock::now() + chrono::milliseconds(10));
  82.     scheduler.Schedule([]() {cout << "30ms" << endl;}, chrono::system_clock::now() + chrono::milliseconds(30));
  83.        
  84.     this_thread::sleep_for(chrono::milliseconds(100));
  85.     return 0;
  86. }
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement