调度

调度在我们生活中无处不在,说简单点就是对任务的合理安排,所以好的调度方法就会有较高的执行效率。


下面这个小例子是一个简单的任务调度;任务调度分为优先级和时间同步,分别将不同的任务存储在不同的链表中,执行时在链表中查找任务、执行。

详见代码注释吧!


任务链表是一个双向链表:

调度_第1张图片


#include <iostream>
using namespace std;

#define Time int

enum 
{
  PRIO1 = 1,
  PRIO2 = 2,
  PRIO3 = 3,
  PRIO_END = 4
};

enum 
{
  SYNC1 = 1,
  SYNC2 = 2,
  SYNC3 = 3,
  SYNC_END = 10   
};

//任务
class Task 
{
public :
  Task(): prev_p_(0), next_p_(0), schedTime_(0)
  {
  };

  virtual ~Task()
  {
    prev_p_ = 0;
    next_p_ = 0;
  };

  virtual void execute() = 0;

private:
  friend class TaskList;
  friend class TaskSched;

  Task *prev_p_;
  Task *next_p_;
  Time schedTime_;
};


/*
任务链表
双向链表
另有指针分别指向此双向链表的头、尾结点         
*/
class TaskList
{
public :

  TaskList() :first_p_(0),last_p_(0)
  {
  }
  ~TaskList()
  {
    first_p_ = last_p_ = 0;
  }
 
  //同一优先级,尾插法
  void insertLast(Task * const task_p)
  {
    //
    task_p->prev_p_ = last_p_;
    task_p->next_p_ = 0;

    (last_p_ != 0 ? last_p_->next_p_ : first_p_) = task_p;
    last_p_ = task_p;
  }
  
  //同一优先级,头部删除
  Task *removeFirst()
  {
    Task *const task_p = first_p_;
    if (task_p == 0)
    {
      //cout<<"No Prio Task to execute."<<endl;
      return 0;
    }

    Task *const next_p = task_p->next_p_;

    first_p_ = next_p;
    (next_p != 0 ? next_p->prev_p_ : last_p_) = 0;

    return task_p;
  }
  //按“时间”插入,按照时间从小到大顺序插入
  void insertSort(Task* const task_p)
  {
    
    Task *prev_p = last_p_;  
    Task *next_p = 0;  

    while (prev_p != 0 && prev_p->schedTime_ > task_p->schedTime_)
    {
	  //临时变量
      next_p = prev_p;
      prev_p = prev_p->prev_p_;
    }

    task_p->prev_p_ = prev_p;
    task_p->next_p_ = next_p;

    (prev_p != 0 ? prev_p->next_p_ : first_p_) = task_p;
    (next_p != 0 ? next_p->prev_p_ : last_p_) = task_p;
  }

  //按"时间"删除,小于executeTime的最小数据将被删除
  Task *removeFirst(const Time &executeTime)
  {
    if (first_p_ == 0 || first_p_->schedTime_ > executeTime)
    {
      //cout<<"No Sync Task to execute."<<endl;
      return 0;
    }

    Task *const oldFirst_p = first_p_;
    Task *const next_p = first_p_->next_p_;

    first_p_ = next_p;
    (next_p != 0 ? next_p->prev_p_ : last_p_) = 0;

    return oldFirst_p;
  }

private:  
  Task *first_p_; 
  Task *last_p_;  
};

//调度器
class TaskSched
{
public:
  TaskSched():synchList_() , prioList_()
  {
  }
  //优先级调度预处理
  void  schedule(Task * const task_p, const int prio)
  { 
    prioList_[prio].insertLast(task_p);
  }

  //执行优先级调度
  bool  executePrioTask()
  {
    //每次从优先级4,3,2,1依次调度
    static int prio = PRIO_END;
    Task *task_p = 0;
    task_p = prioList_[prio].removeFirst();
    if(task_p != 0)
    {
      task_p->execute(); 
      delete task_p;
      return true;
    }
    if(prio > 0)
    {
      prio--;
    }
    return false;
  }  

  //同步调度预处理
  void scheduleSynch(Task* const task_p, int schedTime)
  {
    task_p->schedTime_ = schedTime;
    synchList_.insertSort(task_p);
  }

  //执行同步调度
  void  executeSynchTask()
  {    
    Task *task_p = 0;
    do
    {
      //小于SYNC_END的都将被调度
      task_p = synchList_.removeFirst(SYNC_END);

      if(task_p != 0)
      {
	task_p->execute();
	delete task_p;
      }
    }while(task_p != 0);
  }

  //调度
  void  Do()
  {
    bool prioTaskExecuted = false;

    do
    {
      executeSynchTask();
      //有高优先级任务(prioTaskExecuted == true),再次执行。
      prioTaskExecuted = executePrioTask();

    } while(prioTaskExecuted);  

  }

  //调度实体
  static TaskSched*  instance()
  {
    static TaskSched theInstance;
    return &theInstance;
  }

private:
  TaskList synchList_;  //时间同步表
  TaskList prioList_[3];  //优先级表
 
};

//任务1
class Test1:public Task
{
  public:
  void testSchedule()
  {
    Test1 *task1 = new Test1;
    //加载为优先级调度
    TaskSched::instance()->schedule(task1, PRIO1);
	
    Test1 *task2 = new Test1;
    //加载为优先级调度
    TaskSched::instance()->schedule(task2, PRIO2);
	
    Test1 *task3 = new Test1;
    //加载为优先级调度
    TaskSched::instance()->schedule(task3, PRIO3);
  }
  void execute()
  {
    cout<<"Hello, I'm prio task"<<endl;
  }
};

//任务2
class Test2:public Task
{
  public:
  void testSchedule()
  {
    Test2 *task2 = new Test2;
    //加载为时间同步调度
    TaskSched::instance()->scheduleSynch(task2, SYNC2);
   }
  void execute()
  {
    cout<<"Hello, I'm sync task2"<<endl;
  }
};

//任务3 
class Test3:public Task
{
  public:
  void testSchedule()
  {
    Test3 *task3 = new Test3;
    //加载为时间同步调度
    TaskSched::instance()->scheduleSynch(task3, SYNC1);
  }
  void execute()
  {
    cout<<"Hello, I'm sync task3"<<endl;
  }
}; 
 
int main()
{
  Test1 t1;
  t1.testSchedule();
  Test2 t2;
  t2.testSchedule();
  Test3 t3;
  t3.testSchedule();  
  
  for(int i = 0; i<SYNC_END; i++)
  {
    cout<<" Time = "<<i<<endl;
    TaskSched::instance()->Do();
  }
  return 1;
}


你可能感兴趣的:(调度)