Save week2
parent
f5d40dfc3a
commit
1eb4871631
@ -0,0 +1,30 @@
|
|||||||
|
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()
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
// A race condition occurs in this program because two goroutines are attempting
|
||||||
|
// to access a variable in shared memory `counter` concurrently.
|
||||||
|
// One would expect that the value of coutner at the end of the run would be
|
||||||
|
// 10,000 * 2 = 20,000 and on some runs it will produce this value. On other runs
|
||||||
|
// one gorountine will read the counter value just before the second goroutine
|
||||||
|
// writes to the shared variable. When the first goroutine writes it's counter value + 1
|
||||||
|
// it overwrites the counter value with an older value + 1. This results in some runs
|
||||||
|
// producing a counter result 1000s of values lower than expected.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var counter int
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
goroutine_count := 2
|
||||||
|
|
||||||
|
for i := 0; i < goroutine_count; i++ {
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
counter = counter + 1
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Println("Counter:", counter)
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,36 @@
|
|||||||
|
// A race condition occurs in this program because two goroutines are attempting
|
||||||
|
// to access a variable in shared memory `counter` concurrently.
|
||||||
|
// One would expect that the value of coutner at the end of the run would be
|
||||||
|
// 10,000 * 2 = 20,000 and on some runs it will produce this value. On other runs
|
||||||
|
// one gorountine will read the counter value just before the second goroutine
|
||||||
|
// writes to the shared variable. When the first goroutine writes it's counter value + 1
|
||||||
|
// it overwrites the counter value with an older value + 1. This results in some runs
|
||||||
|
// producing a counter result 1000s of values lower than expected.
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var counter int
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(2)
|
||||||
|
goroutine_count := 2
|
||||||
|
|
||||||
|
for i := 0; i < goroutine_count; i++ {
|
||||||
|
go func() {
|
||||||
|
for i := 0; i < 10000; i++ {
|
||||||
|
counter = counter + 1
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
fmt.Println("Counter:", counter)
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue