You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
31 lines
524 B
Go
31 lines
524 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
/*
|
|
Raca condition is when multiple threads are trying to access and manipulat the same variable.
|
|
In the code below, main, add_one and sub_one are all accessing and changing the value of x.
|
|
Due to the uncertainty of Goroutine scheduling mechanism, the results of the following program is unpredictable.
|
|
*/
|
|
|
|
func add_one(pt *int) {
|
|
(*pt)++
|
|
fmt.Println(*pt)
|
|
}
|
|
|
|
func sub_one(pt *int) {
|
|
(*pt)--
|
|
fmt.Println(*pt)
|
|
}
|
|
func main() {
|
|
i := 0
|
|
go add_one(&i)
|
|
go sub_one(&i)
|
|
|
|
i++
|
|
fmt.Println()
|
|
|
|
}
|