1

Our game have to do daily update in everyday 24:00:00, the code is as follow:

Timer nextDayTimer(boost::asio::io_service& ios) {
    time_t now = time(NULL);
    struct tm tm = {0};
    localtime_r(&now, &tm);
    tm.tm_hour = 0;
    tm.tm_min = 0;
    tm.tm_sec = 0;

    time_t next = mktime(&tm);
    if (next <= now) {
      tm.tm_mday++;
      next = mktime(&tm);
    }

    Timer timer = Timer(new boost::asio::deadline_timer(ios));
    int diff = (int) (diffTime(next, now) * 1000.0);
    timer->expires_from_now(boost::posix_time::milliseconds(diff));
    return timer;
}

Then do daily update when reach the time that the timer expires. and Then call nextDayTimer again;

However, I found there are some wrong with it, it occasionally do daily update twice in 24:00:00, what reason would lead to this?

suzuiyue
  • 156
  • 1
  • 1
  • 12

0 Answers0