-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
race condition pointers example solutions added in a file. Benchmark …
…tests added to compare solutions. Test running guide added to Readme file.
- Loading branch information
Showing
5 changed files
with
110 additions
and
31 deletions.
There are no files selected for viewing
31 changes: 0 additions & 31 deletions
31
...utines/race-condition-pointers-fix-with-atomic/race-condition-pointers-fix-with-atomic.go
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
102-concurrency/goroutines/race-condition-pointers-fix/README.md
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
## How to run Benchmark | ||
|
||
* Run this command for Atomic Example. | ||
|
||
`$go test -benchmem -run=^$ -bench ^BenchmarkAtomic$` | ||
|
||
* Run this command for Mutex Lock Example. | ||
|
||
`$go test -benchmem -run=^$ -bench ^BenchmarkMutexLock$` | ||
|
||
### Example Results | ||
|
||
* **Atomic** | ||
* goos: darwin | ||
* goarch: amd64 | ||
* pkg: raceConditionPointersFix | ||
* cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz | ||
* BenchmarkAtomic-4 --- 4977807 --- 236.2 ns/op --- 24 B/op --- 1 allocs/op | ||
* PASS | ||
* ok raceConditionPointersFix 1.895s | ||
|
||
|
||
* **Mutex Lock** | ||
* goos: darwin | ||
* goarch: amd64 | ||
* pkg: raceConditionPointersFix | ||
* cpu: Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz | ||
* BenchmarkMutexLock-4 --- 4626063 --- 260.3 ns/op --- 33 B/op 1 allocs/op | ||
* PASS | ||
* ok raceConditionPointersFix 1.592s | ||
|
3 changes: 3 additions & 0 deletions
3
102-concurrency/goroutines/race-condition-pointers-fix/go.mod
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module raceConditionPointersFix | ||
|
||
go 1.17 |
41 changes: 41 additions & 0 deletions
41
102-concurrency/goroutines/race-condition-pointers-fix/race-condition-pointers-fix.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package raceConditionPointersFix | ||
|
||
import ( | ||
"fmt" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
type RaceTest struct { | ||
Val int32 | ||
} | ||
|
||
func main() { | ||
raceTest := &RaceTest{} | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(10000) | ||
|
||
// mtx := &sync.Mutex{} // This line for mutex lock method | ||
|
||
for i:=0; i<10000; i++ { | ||
go incrementWithAtomic(raceTest, wg) | ||
// go incrementWithLock(raceTest, wg, mtx) // This line for mutex lock method | ||
} | ||
|
||
wg.Wait() | ||
|
||
fmt.Println(raceTest) | ||
} | ||
|
||
func incrementWithAtomic(rt *RaceTest, wg *sync.WaitGroup) { | ||
atomic.AddInt32(&rt.Val, 1) | ||
wg.Done() | ||
} | ||
|
||
func incrementWithLock(rt *RaceTest, wg *sync.WaitGroup, mtx *sync.Mutex) { | ||
mtx.Lock() | ||
rt.Val += 1 | ||
mtx.Unlock() | ||
wg.Done() | ||
} |
35 changes: 35 additions & 0 deletions
35
102-concurrency/goroutines/race-condition-pointers-fix/race-condition-pointers-fix_test.go
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package raceConditionPointersFix | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
) | ||
|
||
// go test -benchmem -run=^$ -bench ^BenchmarkAtomic$ | ||
func BenchmarkAtomic(b *testing.B) { | ||
raceTest := &RaceTest{} | ||
wg := &sync.WaitGroup{} | ||
wg.Add(b.N) | ||
|
||
for i := 0; i < b.N; i++ { | ||
go incrementWithAtomic(raceTest, wg) | ||
} | ||
|
||
wg.Wait() | ||
} | ||
|
||
// go test -benchmem -run=^$ -bench ^BenchmarkMutexLock$ | ||
func BenchmarkMutexLock(b *testing.B) { | ||
raceTest := &RaceTest{} | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(b.N) | ||
|
||
mtx := &sync.Mutex{} | ||
|
||
for i := 0; i < b.N; i++ { | ||
go incrementWithLock(raceTest, wg, mtx) | ||
} | ||
|
||
wg.Wait() | ||
} |