Skip to content
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

time: Timer with zero Duration seems to have a moment of delay #10124

Closed
binary132 opened this issue Mar 10, 2015 · 2 comments
Closed

time: Timer with zero Duration seems to have a moment of delay #10124

binary132 opened this issue Mar 10, 2015 · 2 comments

Comments

@binary132
Copy link

@binary132 binary132 commented Mar 10, 2015

http://play.golang.org/p/9saBM6ph-9

There seems to be a small delay incurred when Timer starts. Note the "sync" workaround in notBroken. This behavior was noticed on go1.2.1 linux/amd64 and is repeatable on play.golang.org and go1.4.1 linux/amd64.

The following program outputs:

This is the undesired behavior.

This is the desired select.
package main

import "time"

func main() {
    broken()
    println()
    notBroken()
}

func broken() {
    t := time.NewTimer(0 * time.Second)

    select {
    case _ = <-t.C:
        println("This is the desired select.")
    default:
        println("This is the undesired behavior.")
    }
}

func notBroken() {
    t := time.NewTimer(0 * time.Second)

    sync := time.NewTimer(0 * time.Second)
    _ = <-sync.C

    select {
    case _ = <-t.C:
        println("This is the desired select.")
    default:
        println("This does not get selected.")
    }
}
@binary132 binary132 changed the title time.Timer with zero Duration seems to have a moment of delay time: Timer with zero Duration seems to have a moment of delay Mar 10, 2015
@minux
Copy link
Member

@minux minux commented Mar 10, 2015

@minux minux closed this Mar 10, 2015
@dvyukov
Copy link
Member

@dvyukov dvyukov commented Mar 10, 2015

time package timers fire after "at least the duration d":
http://golang.org/pkg/time/#Sleep
We don't special case zero duration as it looks pointless.

@golang golang locked and limited conversation to collaborators Jun 25, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.