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

sync/atomic: sequence of execution #42917

Closed
Zh1Cheung opened this issue Dec 1, 2020 · 2 comments
Closed

sync/atomic: sequence of execution #42917

Zh1Cheung opened this issue Dec 1, 2020 · 2 comments

Comments

@Zh1Cheung
Copy link

Zh1Cheung commented Dec 1, 2020

For the official implementation of Go 1.15, the order between the variables of Load/Store using atomic can be guaranteed.

Package atomic provides low-level atomic memory primitives useful for implementing synchronization algorithms.

The load and store operations, implemented by the LoadT and StoreT functions, are the atomic equivalents of "return *addr" and "*addr = val".


func main() {
	var a, b int32 = 0, 0

	go func() {
		atomic.StoreInt32(&a, 1)
		atomic.StoreInt32(&b, 1)
	}()

	for atomic.LoadInt32(&b) == 0 {
		runtime.Gosched()
	}
	fmt.Println(atomic.LoadInt32(&a))

}

The printed result of a is always 1, but it is not reflected in the document.

@Zh1Cheung Zh1Cheung changed the title go/doc go/doc:Sequence of execution in atomic Dec 1, 2020
@dmitshur dmitshur changed the title go/doc:Sequence of execution in atomic sync/atomic: sequence of execution Dec 1, 2020
@go101
Copy link

go101 commented Dec 1, 2020

see #5045

@ianlancetaylor
Copy link
Contributor

Closing this issue as a duplicate of #5045.

@golang golang locked and limited conversation to collaborators Dec 2, 2021
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

4 participants