Last active
May 27, 2016 01:37
-
-
Save chadlung/755401a64910125cca8f67b695c61c4d to your computer and use it in GitHub Desktop.
Introducing a race condition with Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
// This code introduces a race condition!!! | |
finished := make(chan bool, 1) | |
var i int | |
go func() { | |
fmt.Println("In the Go routine reading to increment i") | |
i++ | |
fmt.Println("New value of i:", i) | |
finished <- true | |
}() | |
i++ | |
<-finished | |
fmt.Println("Finished!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment