Timer C++;任务调度器 我想在C++(Linux环境下)编写一个作业调度程序,它在JobID和Job(STD::MAP)的映射中调用,并在到达预定时间后调用与该作业相关联的函数。p>

Timer C++;任务调度器 我想在C++(Linux环境下)编写一个作业调度程序,它在JobID和Job(STD::MAP)的映射中调用,并在到达预定时间后调用与该作业相关联的函数。p>,timer,task,scheduler,Timer,Task,Scheduler,作业数据结构如下所示: enum Type {Scheduled,Periodic,Event}; typedef int JobId; class View { void callback(T results); }; typedef vector<View> Views; typename <class T> typedef struct { Type type; unsigned int epochTime; unsigned

作业数据结构如下所示:

enum Type {Scheduled,Periodic,Event};
typedef int JobId;

class View
{
    void callback(T results);
};

typedef vector<View> Views;

typename <class T>
typedef struct
{
    Type type;
    unsigned int epochTime;
    unsigned int period;
    std::function func;
    int timeout;
    T result;
    Views views;
}Job;

std::map<JobId, Job> Jobs;      //Data structure to add a job schedule
与“定期”作业相关的功能是:

与“事件”作业关联的功能是:

在main函数中,我应该能够如下创建作业计划:(参见main函数中的注释)

这将在1538390376历元时间异步执行函数scheduledTime。函数add需要2秒钟来处理结果。一旦结果可用,应使用回调函数通知订阅者(此处为view1)

    //Scenario 2 (Periodic Timer)
    Job job2;
    Views view11,view12;
    job2.type = Periodic;
    job2.epochTime = 0;
    job1.timeout = 15; 
    job2.period = 10;
    job2.func = periodic;
    job2.views.insert(view21);
    job2.views.insert(view22);

    jobs[2] = job2; 
    //Scenario 3 (Event Timer)
    Job job3;
    Views view31,view32;
    job3.type = Event;
    job3.epochTime = 0;
    job1.timeout = 20; 
    job3.period = 5;
    job3.func = eventbased;
    job3.views.insert(view31);
    job3.views.insert(view32);

    jobs[3] = job3; 
}
这应该每隔10秒异步执行一次函数。函数add需要5秒钟来处理结果。一旦结果可用,应使用回调函数通知订户(此处为view21和view22)

    //Scenario 2 (Periodic Timer)
    Job job2;
    Views view11,view12;
    job2.type = Periodic;
    job2.epochTime = 0;
    job1.timeout = 15; 
    job2.period = 10;
    job2.func = periodic;
    job2.views.insert(view21);
    job2.views.insert(view22);

    jobs[2] = job2; 
    //Scenario 3 (Event Timer)
    Job job3;
    Views view31,view32;
    job3.type = Event;
    job3.epochTime = 0;
    job1.timeout = 20; 
    job3.period = 5;
    job3.func = eventbased;
    job3.views.insert(view31);
    job3.views.insert(view32);

    jobs[3] = job3; 
}
这应该在5秒后异步执行函数eventbased。基于eventbased的函数需要15秒才能执行,之后应使用回调函数通知订阅者(此处为view31和view32)

    //Scenario 2 (Periodic Timer)
    Job job2;
    Views view11,view12;
    job2.type = Periodic;
    job2.epochTime = 0;
    job1.timeout = 15; 
    job2.period = 10;
    job2.func = periodic;
    job2.views.insert(view21);
    job2.views.insert(view22);

    jobs[2] = job2; 
    //Scenario 3 (Event Timer)
    Job job3;
    Views view31,view32;
    job3.type = Event;
    job3.epochTime = 0;
    job1.timeout = 20; 
    job3.period = 5;
    job3.func = eventbased;
    job3.views.insert(view31);
    job3.views.insert(view32);

    jobs[3] = job3; 
}

注意:可以动态添加/删除/更新作业,而无需重新启动现有正在运行的计划程序。

您可以使用这些想法启动一个开源项目!如果您需要GitHub上的组织,欢迎使用。但是,请随意创建您想要的。此外,您可能已经意识到类似的Initialive: