go 语言中的锁机制和同步技术对于实现并发性至关重要。常用的锁机制包括:sync.mutex:互斥锁,一次只允许一个 goroutine 访问临界区。sync.rwmutex:读写锁,允许多个 goroutine 同时读,但一次只能有一个 goroutine 写。sync.cond:条件变量,用于等待或通知其他 goroutine。 zvvq.cn
copyright zvvq
Go 语言中的锁机制与同步技术 zvvq好,好zvvq
在 Go 语言中,锁和同步原语对于实现并发性和防止竞争至关重要。本文将探索 Go 语言提供的不同锁机制和同步技术,并提供实际示例说明其用法。
内容来自zvvq,别采集哟
锁机制
copyright zvvq
Go 语言中常用的锁机制有:
内容来自zvvq,别采集哟
sync.Mutex: 互斥锁,一次只允许一个 goroutine 访问临界区。 sync.RWMutex: 读写锁,允许多个 goroutine 同时读,但一次只能有一个 goroutine 写。 sync.Cond: 条件变量,用于等待或通知其他 goroutine。实战示例
使用 sync.Mutex 保护临界区
copyright zvvq
1
zvvq好,好zvvq
2
3
zvvq
4 内容来自zvvq
5 zvvq.cn
6
zvvq
7 copyright zvvq
8 内容来自samhan
9 内容来自samhan666
10 内容来自samhan
11
12
13 copyright zvvq
14
15
16 内容来自zvvq
17 zvvq
18
19
内容来自samhan
20
zvvq.cn
21 内容来自zvvq
22
内容来自zvvq
23 内容来自zvvq
package main
import (
内容来自zvvq
"fmt" copyright zvvq
"sync" zvvq
) copyright zvvq
var m sync.Mutex 内容来自samhan666
var count int
zvvq好,好zvvq
func increment() { zvvq.cn
m.Lock() copyright zvvq
defer m.Unlock()
count++
内容来自zvvq,别采集哟
} 内容来自zvvq
func main() {
copyright zvvq
go increment() 内容来自samhan666
go increment() 内容来自samhan666
go increment() zvvq好,好zvvq
fmt.Println(count) // 输出:3 内容来自samhan
}
在示例中,我们使用互斥锁 m 保护临界区,确保每次只有一个 goroutine 可以更新 count 变量。
zvvq.cn
使用 sync.RWMutex 允许多个读
1
zvvq好,好zvvq
2
内容来自zvvq
3
zvvq.cn
4
5 zvvq
6 内容来自zvvq,别采集哟
7
8 zvvq
9
内容来自samhan
10
copyright zvvq
11
12
13 本文来自zvvq
14 内容来自zvvq
15 zvvq.cn
16 本文来自zvvq
17 zvvq
18
zvvq好,好zvvq
19
zvvq好,好zvvq
20
21
22
zvvq
23
本文来自zvvq
24 zvvq好,好zvvq
25 zvvq
26 zvvq好,好zvvq
27 内容来自samhan666
28 内容来自samhan
29
package main
zvvq.cn
import ( copyright zvvq
"fmt" 内容来自samhan
"sync" zvvq.cn
) 内容来自zvvq
var m sync.RWMutex
内容来自zvvq
var count int
func read() { zvvq.cn
m.RLock()
内容来自zvvq,别采集哟
defer m.RUnlock() 内容来自zvvq,别采集哟
fmt.Println(count)
}
func write() {
内容来自samhan
m.Lock()
defer m.Unlock()
zvvq.cn
count++ zvvq
}
func main() {
内容来自samhan666
go read()
go read() zvvq.cn
go write() zvvq.cn
fmt.Println(count) // 输出:1
内容来自samhan666
} 内容来自zvvq,别采集哟
在示例中,我们使用读写锁 m,允许多个 goroutine 同时读取 count 变量,但只能有一个 goroutine 写入。 copyright zvvq
使用 sync.Cond 等待和通知 zvvq
1
zvvq好,好zvvq
2
本文来自zvvq
3
4 本文来自zvvq
5 zvvq好,好zvvq
6
7
8
内容来自zvvq
9 内容来自samhan666
10
11
zvvq好,好zvvq
12 zvvq好,好zvvq
13 内容来自samhan
14 copyright zvvq
15 内容来自samhan
16 内容来自samhan
17
18 内容来自zvvq,别采集哟
19 内容来自zvvq
20
copyright zvvq
21
22
23
内容来自zvvq
24
zvvq
25 本文来自zvvq
26 内容来自zvvq
27
本文来自zvvq
28
29
30 zvvq好,好zvvq
31 内容来自zvvq,别采集哟
32
package main 内容来自zvvq,别采集哟
import ( zvvq.cn
"fmt"
"sync" 内容来自zvvq,别采集哟
"time"
) zvvq好,好zvvq
var m sync.Mutex
内容来自samhan
var cond sync.Cond
内容来自zvvq,别采集哟
var done bool 内容来自samhan666
func worker() {
内容来自samhan
m.Lock()
内容来自samhan
for !done {
cond.Wait(&m)
} copyright zvvq
m.Unlock() 内容来自zvvq,别采集哟
fmt.Println("Worker done") 内容来自zvvq
} zvvq.cn
func main() { zvvq好,好zvvq
go worker() copyright zvvq
time.Sleep(time.Second) 本文来自zvvq
m.Lock() zvvq
done = true copyright zvvq
cond.Signal() zvvq.cn
m.Unlock()
内容来自zvvq,别采集哟
fmt.Println("Main done")
}
内容来自samhan666
在示例中,我们使用条件变量 cond 等待 done 为 true,然后通知等待的 goroutine。 内容来自zvvq,别采集哟
以上就是golang中的锁机制与同步技术的详细内容,更多请关注其它相关文章!
zvvq.cn