Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 155c2a7

Browse files
slightly adjust mutexes examples
1 parent 3efdd92 commit 155c2a7

File tree

36 files changed

+412
-245
lines changed

36 files changed

+412
-245
lines changed

‎README.md‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@
6363
- [Map - sync.Map](https://github.com/golang-basics/concurrency/tree/master/mutexes/syncmap)
6464
- [Cond - sync.Cond](https://github.com/golang-basics/concurrency/tree/master/mutexes/cond)
6565
- [Once - sync.Once](https://github.com/golang-basics/concurrency/tree/master/mutexes/once)
66-
- [Locker - sync.Locker](https://github.com/golang-basics/concurrency/tree/master/mutexes/synclocker)
6766
- [Race Conditions](https://github.com/golang-basics/concurrency/tree/master/go-routines/race-condition)
6867
- [Threads](https://github.com/golang-basics/concurrency/tree/master/threads)
6968
- [GOMAXPROCS](https://github.com/golang-basics/concurrency/tree/master/gomaxprocs)

‎mutexes/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ the database consistency which otherwise is not possible for a non-serial schedu
6060

6161
### Presentations
6262

63-
- [Concurrency in Go #6 - Mutexes](https://github.com/golang-basics/concurrency/raw/master/presentations/6_mutexes)
63+
- [Concurrency in Go #7 - Mutexes](https://github.com/golang-basics/concurrency/raw/master/presentations/7_mutexes)
6464

6565
### Examples
6666

‎mutexes/atomic-waitgroup/main.go‎

Lines changed: 0 additions & 27 deletions
This file was deleted.

‎mutexes/basic-mutex/main.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,32 @@
11
package main
22

3+
import (
4+
"fmt"
5+
"sync"
6+
"time"
7+
)
8+
9+
// to check for race conditions use the -race flag
10+
// go run -race main.go
311
func main() {
12+
var count int
13+
var mu sync.Mutex
14+
go func() {
15+
mu.Lock()
16+
count = 10
17+
mu.Unlock()
18+
}()
19+
20+
// remember: the main function is also running as a go routine
21+
// a good rule of thumb is to avoid RW conflicts in the main
22+
// and keep all the concurrent operations in their own go routines
23+
mu.Lock()
24+
count = 15
25+
mu.Unlock()
26+
27+
// to avoid using WaitGroup
28+
time.Sleep(time.Second)
29+
mu.Lock()
30+
fmt.Println("count", count)
31+
mu.Unlock()
432
}

‎mutexes/context/coarse-grained-context/main.go‎ renamed to ‎mutexes/contexts/coarse-grained-context/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
defer wg.Done()
1717
var localCount int
1818
mu.Lock()
19-
localCount = count+1
19+
localCount = count+1
2020
count = localCount
2121
mu.Unlock()
2222
}()

‎mutexes/context/fine-grained-context/main.go‎ renamed to ‎mutexes/contexts/fine-grained-context/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
defer wg.Done()
1717
var localCount int
1818
mu.Lock()
19-
localCount = count+1
19+
localCount = count+1
2020
mu.Unlock()
2121
mu.Lock()
2222
count = localCount

‎mutexes/context/multiple-gouroutines/main.go‎ renamed to ‎mutexes/contexts/multiple-gouroutines-readonly/main.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func main() {
1616
defer wg.Done()
1717
// Each go routine only reads the count data and does not expose its data outside
1818
var localCount int
19-
localCount += count+i+1
19+
localCount += count+i+1
2020
fmt.Println("local count", localCount)
2121
}(i)
2222
}

‎mutexes/context/single-gouroutine/main.go‎ renamed to ‎mutexes/contexts/single-gouroutine/main.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ func main() {
1717
// Only the G1 go routine will do the write operation, thus it's safe
1818
count++
1919
}()
20-
// Main G does not read or write while G1 is running
21-
// So this will not run in a race condition
20+
// Main G does not read or write while G1 is running,
21+
// so this will not run in a race condition.
22+
// Or we could just move count++ inside main, which will execute in its own go routine
2223

2324
wg.Wait()
2425
fmt.Println("count", count)
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
// to check for race conditions use the -race flag
9+
// go run -race main.go
10+
func main() {
11+
d := newDB()
12+
var wg sync.WaitGroup
13+
wg.Add(2)
14+
go func() {
15+
// setting k1 here
16+
d.set("k1", "v1")
17+
wg.Done()
18+
}()
19+
go func() {
20+
// setting k1 as well here
21+
d.set("k1", "v2")
22+
wg.Done()
23+
}()
24+
// trying to get a value while it's being set by the go routines
25+
fmt.Println(d.get("k1"))
26+
wg.Wait()
27+
}
28+
29+
func newDB() db {
30+
return db{values: map[string]string{}}
31+
}
32+
33+
type db struct {
34+
values map[string]string
35+
mu sync.Mutex
36+
}
37+
38+
func (d *db) set(key, value string) {
39+
d.mu.Lock()
40+
defer d.mu.Unlock()
41+
d.values[key] = value
42+
}
43+
44+
func (d *db) get(key string) string {
45+
d.mu.Lock()
46+
defer d.mu.Unlock()
47+
return d.values[key]
48+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /