Skip to content

Abnormal number increment #46098

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

Closed
storyicon opened this issue May 11, 2021 · 1 comment
Closed

Abnormal number increment #46098

storyicon opened this issue May 11, 2021 · 1 comment

Comments

@storyicon
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.16.3 windows/amd64

Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\storyicon\AppData\Local\go-build
set GOENV=C:\Users\storyicon\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=D:\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=D:\Go
set GOPRIVATE=
set GOROOT=D:\Program Files\golang
set GOSUMDB=off
set GOTMPDIR=
set GOTOOLDIR=D:\Program Files\golang\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\STORYI~1\AppData\Local\Temp\go-build2449719716=/tmp/go-build -gno-record-gcc-switches

What did you do?

image

package main

import (
	"fmt"
	"time"
)

func main(){
	i := 0
	go func() {
		for {
			i++
		}
	}()
	ticker := time.NewTicker(time.Second)
	defer ticker.Stop()
	for {
		select {
		case <-ticker.C:
			fmt.Printf("i=%d \r\n", i)
		}
	}
}

What did you expect to see?

i should be increasing

What did you see instead?

i is always equal to 0

This seems like a very simple problem, but it doesn't meet intuitive expectations.

@davecheney
Copy link
Contributor

Thank you for raising this issue, however this is not a bug, your program has a data race.

Have you tried running your program under the race detector? See https://blog.golang.org/race-detector .

Unlike many projects, the Go project does not use GitHub Issues for general discussion or asking questions. GitHub Issues are used for tracking bugs and proposals only.

For asking questions, see:

@golang golang locked and limited conversation to collaborators May 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants