Bosma / Scheduler

Modern C++ Scheduling Library
MIT License
273 stars 75 forks source link

at method run at the begin of execution. #8

Closed klukaspl closed 6 years ago

klukaspl commented 6 years ago

Hi,

Is it normal behavior that the function called like for instance: s.at("2018-02-27 15:39:30", []() { std::cout << "at a specific time. RELAY ON" << std::endl; });

will print at a specific time. RELAY ON not only at 2018-02-27 15:39:30 but as well just after program start execution. I my case it seems to be just like that, but I expected to print only on the specified time. Do you have any hint how to make it working?

I can ensure that my system time is before of the set time in the method call.

BR

Krzysztof

Bosma commented 6 years ago

s.at("2018-02-27 15:39:30", []() { std::cout << "at a specific time. RELAY ON" << std::endl; });

I can't see any way for at on its own to fire the function twice. I would need you to provide a MCVE. The only way I see at firing the function just after program start is if the time provided is before the program start time. But if it's fired, it's removed from the tasks queue (and doesn't get added back).

You should be able to verify manually by adding the following function to the Scheduler class and calling it after adding the task and after the task fires:

void print_tasks() {
  std::lock_guard<std::mutex> l(lock);
  if (tasks.empty()) {
    std::cout << "Empty" << std::endl;
  } else {
    for (const auto &task : tasks) {
      std::cout << task.first.time_since_epoch().count() << std::endl;
    }
  }
}

I should make tests, but it's just a little difficult for me due to everything being temporal and multithreaded.

klukaspl commented 6 years ago

Hi, yes probably it was my mistake here - I probably set the date after the one called in at function, because now I am not able to reproduce the problem.

In case of code - I was using the example from repository - just change the data in the at function.

Thank you very much!

BR