-
Notifications
You must be signed in to change notification settings - Fork 586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Is there a defect in the Timer implementation? #185
Comments
This implementation is too complex, and even with a delay, accuracy cannot be guaranteed. What are the scenarios where execution at a specific time is absolutely necessary?
|
It is not executed at a specific time, but rather executes a fixed task every N seconds, such as a fixed interval heartbeat; the implementation is not complicated, one method is to count the time as mentioned above, and subtract the time in the implementation, another method is to record the timestamp, and use now - timestamp each time epoll_wait is called.
|
You can refer to the implementation: https://github.com/chenshuo/muduo/blob/master/muduo/net/TimerQueue.cc
|
Executing time-consuming tasks in a timer itself does not conform to the specifications of asynchronous non-blocking programming. We assume that all tasks are asynchronous as a design premise. Slight execution delays will not have a significant impact on accuracy. In practice, timer tasks that are very sensitive to delays and require millisecond-level accuracy are very rare. Moreover, non-real-time operating system kernels cannot guarantee real-time scheduling. It is also possible that the accuracy is not sufficient due to untimely scheduling. I think it is not worthwhile to consider adding these logic for this reason.
|
Moreover, zltoolkit guarantees the execution delay between tasks, which cannot be considered a bug. If a task occasionally experiences a delay of 300ms, but its interval is set to 500ms, according to your settings, the next execution should be in 200ms. However, is this really what the user expects? I don't think so.
|
It makes sense to design with asynchronous tasks as a premise, but as a basic library, the behavior is not consistent with expectations. From the concept of a timer, understanding that executing once every second means once every second (of course, there may be errors), otherwise it is not a timer; for example, using this timer to control video encoding to 60fps, if the encoding time itself is ignored, the actual encoding frame rate will deviate a lot; of course, what I am saying is based on the premise that the basic library is used by others, and the caller's intention cannot be guessed, the concept needs to be clear.
|
You can also refer to the logic of the timerfd in the Linux system, its behavior is consistent with what I expect. ~
|
Issue:
The implementation of Timer does not consider the time spent executing the callback itself. For example, if a timed task is scheduled to execute every 1 second, and each execution takes 500 milliseconds, the actual execution interval will become 1000 + 500 = 1500 milliseconds according to the current logic.
Solution:
Statistically track the actual time spent executing the callback. In the next
doDelayTask
, subtract the time spent in the current execution. If the callback execution time exceeds the scheduled interval, execute the task immediately next time.TRANS_BY_GITHUB_AI_ASSISTANT
The text was updated successfully, but these errors were encountered: