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

runtime: why a variable in closure can not be read in time? #33591

Closed
chenqinghe opened this issue Aug 12, 2019 · 4 comments
Closed

runtime: why a variable in closure can not be read in time? #33591

chenqinghe opened this issue Aug 12, 2019 · 4 comments

Comments

@chenqinghe
Copy link

@chenqinghe chenqinghe commented Aug 12, 2019

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

$ go version
go version go1.11 windows/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\username\AppData\Local\go-build
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=E:\libs;E:\projects
set GOPROXY=
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\username\AppData\Local\Temp\go-build715944026=/tmp/go-build -gno-record-gcc-switches

What did you do?

here is the code:

package main

import (
	"fmt"
	"sync"
)

func main() {
	var (
		i int
		wg sync.WaitGroup
	)

	wg.Add(2)

	go func() {
		for {
			i++
		}
	}()

	go func() {
		for {
			fmt.Println(i)
		}
	}()

	wg.Wait()
}

my computer has 4 logic cpu, so these two goroutines may both have opportunity to run, but unfortunately, what was printed is always 0.

What did you expect to see?

print different value of i

What did you see instead?

always print 0

@bserdar

This comment has been minimized.

Copy link

@bserdar bserdar commented Aug 12, 2019

The two goroutines do not communicate, so there is no guarantee that something happens in one is visible to the other.

https://golang.org/ref/mem

@Yanwenjiepy

This comment has been minimized.

Copy link

@Yanwenjiepy Yanwenjiepy commented Aug 12, 2019

Both goroutines are running, but i in main is always 0, and the goroutine that executes i++ does not copy an i to perform the add operation.
Is i++ been always executed by cpu, but the result is not updated to i in main?

If you execute another call in a goroutine that executes i++, such as time.sleep(), the value of i in main will be updated, and another goroutine will get the value of i.

Or you pass &i into these two goroutines, you can also find that the value of i in main is updated, and another goroutine can also get the value of i.

So are these phenomena related to the go compiler or runtime?
Is it also related to the data communication between the computer's cpu and memory?

@Yanwenjiepy

This comment has been minimized.

Copy link

@Yanwenjiepy Yanwenjiepy commented Aug 12, 2019

The two goroutines do not communicate, so there is no guarantee that something happens in one is visible to the other.

https://golang.org/ref/mem

3Q

@av86743

This comment has been minimized.

Copy link

@av86743 av86743 commented Aug 12, 2019

Dup of #10958.

@chenqinghe chenqinghe closed this Aug 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.