From 1eb487163114b78870d648b5df856fca30c5656f Mon Sep 17 00:00:00 2001 From: Drew Bednar Date: Mon, 9 Oct 2023 11:24:50 -0400 Subject: [PATCH] Save week2 --- .../week2/grading/student1_race.go | 30 ++++++++++++++++ coursera/third_course/week2/race.go | 36 +++++++++++++++++++ coursera/third_course/week2/race.go.bak | 36 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 coursera/third_course/week2/grading/student1_race.go create mode 100644 coursera/third_course/week2/race.go create mode 100644 coursera/third_course/week2/race.go.bak diff --git a/coursera/third_course/week2/grading/student1_race.go b/coursera/third_course/week2/grading/student1_race.go new file mode 100644 index 0000000..e606d1a --- /dev/null +++ b/coursera/third_course/week2/grading/student1_race.go @@ -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() + +} diff --git a/coursera/third_course/week2/race.go b/coursera/third_course/week2/race.go new file mode 100644 index 0000000..38be96d --- /dev/null +++ b/coursera/third_course/week2/race.go @@ -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) + +} diff --git a/coursera/third_course/week2/race.go.bak b/coursera/third_course/week2/race.go.bak new file mode 100644 index 0000000..38be96d --- /dev/null +++ b/coursera/third_course/week2/race.go.bak @@ -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) + +}