7

I'm trying to use Go's time.Timers to schedule tasks that need to be run in the right order with a precision in the order of half a millisecond. This works perfectly fine on OSX and on Linux, but fails every time on Windows.

The following code demonstrates the issue. It sets 5 timers, the first one to 1 ms, the second to 2 ms, ..., and the last one to 5 ms. Once a timer fires, its number is printed. On OSX and Linux, this obviously produced "12345" as output, but on Windows the numbers are more or less random (tested on Win 7 and Windows Server 2012).

package main

import (
    "fmt"
    "time"
)

func main() {
    var timer1, timer2, timer3, timer4, timer5 *time.Timer

    timer1 = time.NewTimer(1 * time.Millisecond)
    timer2 = time.NewTimer(2 * time.Millisecond)
    timer3 = time.NewTimer(3 * time.Millisecond)
    timer4 = time.NewTimer(4 * time.Millisecond)
    timer5 = time.NewTimer(5 * time.Millisecond)

    // should print 12345
    for {
        select {
        case <-timer1.C:
            fmt.Print("1")
        case <-timer2.C:
            fmt.Print("2")
        case <-timer3.C:
            fmt.Print("3")
        case <-timer4.C:
            fmt.Print("4")
        case <-timer5.C:
            fmt.Print("5")
        case <-time.After(200 * time.Millisecond):
            return // exit the program
        }
    }
}

I think this behavior is due to the changes made in Go 1.6 (https://golang.org/doc/go1.6#runtime, 4th paragraph), where the Windows timer precision was reduced from 1 ms to 16 ms, although it should also have occurred with shorter intervals (of the order of 100 μs) before.

Is there any way to reset the global Windows timer precision back to 1 ms, or to access a high resolution timer that would make the example above work?

m4r73n
  • 756
  • 13
  • 21
  • 3
    What is the current behavior in the go1.7rc? There have been more changes around the windows timers due to their performance. You may want to read through the many windows timer related issues in GH. – JimB Jun 08 '16 at 15:45
  • Timers seem to have a higher resolution with the current beta of Go 1.7. Thanks for your help. – m4r73n Jun 09 '16 at 05:39
  • 1
    It's probably a good idea to add that as an answer and mark it as accepted if you're happy with it. Then people looking to answer unanswered questions can skip this. – ijt Mar 05 '17 at 06:28

3 Answers3

3

Since Go 1.7, timers now have a higher resolution and this problem should not occur.

timotree
  • 1,325
  • 9
  • 29
0

Several years later... this problem still occurs on Go 1.18.

also if I print the duration of the select statement, I get between 210 and 220 ms, so there is a 10-20 ms jitter in the timer (or in the channel communication...)

Eric
  • 1
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/34202865) – Rohit Gupta Apr 17 '23 at 04:49
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 21 '23 at 06:16
-1

Golang derives the resolution of time from the OS. This is regardless of the version.

see How precise is Go's time, really?

Brinan
  • 1
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/34746344) – JimB Jul 27 '23 at 13:33
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 01 '23 at 11:12