Here's a rather hastily made time scheduler to suit my requirements and even for my future projects. It is working as intended but I want to improve more . I am a self taught enthusiast, and rather weak as a language lawyer.
This scheduler should be able to schedule tasks which will be executed the task at the right instance. It should arrange the time-points in chronological(//TODO boundary condition) order.
typedef std::function<void(void)> func;
typedef std::chrono::system_clock::time_point time_point;
std::map<time_point, func> schedule;
int main()
{
schedule.insert(make_pair(std::chrono::system_clock::now() + std::chrono::seconds(4s), []() { std::cout << " Slow first \n"; }));
schedule.insert(make_pair(std::chrono::system_clock::now() + std::chrono::seconds(3s), []() { std::cout << " Medium second \n"; }));
thread t([]()
{
while (!schedule.empty())
{
if (chrono::system_clock::now() >= (schedule.begin())->first)
{
(schedule.begin())->second();
schedule.erase(schedule.begin());
}
this_thread::sleep_for(chrono::milliseconds(1ms));
}
});
schedule.insert(make_pair(std::chrono::system_clock::now() + std::chrono::milliseconds(500ms), []() { std::cout << " Impatience third \n"; }));
std::cout << " main is waiting for the thread to end already \n";
t.join();
return 0;
}