并发编程
字数
729 字
阅读时间
4 分钟
用法
Goroutines
```go
func say(s string) {
for i := 0; i < 5; i++ {
time.Sleep(100 * time.Millisecond)
fmt.Println(s)
}
}
func main() {
go say("world")
say("hello")
}Channels
1. 基本使用
go
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // send sum to c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // receive from c
fmt.Println(x, y, x+y)
}2. 有缓冲的 channel
go
ch := make(chan int, 100)
ch <- 1
ch <- 2
fmt.Println(<-ch)
fmt.Println(<-ch)Range and Close
go
func main() {
c := make(chan int, 10)
go fibonacci(cap(c), c)
// 可以用 range 遍历 channel,直到 channel 关闭。
for i := range c {
fmt.Println(i)
}
}
func fibonacci(n int, c chan int) {
x, y := 0, 1
for i := 0; i < n; i++ {
c <- x
x, y = y, x+y
}
// 关闭 channel
close(c)
}使用 Select
go
func fibonacci(c, quit chan int) {
x, y := 0, 1
for {
select {
case c <- x:
x, y = y, x+y
case <-quit:
fmt.Println("quit")
return
}
}
}
func main() {
c := make(chan int)
quit := make(chan int)
go func() {
for i := 0; i < 10; i++ {
fmt.Println(<-c)
}
quit <- 0
}()
fibonacci(c, quit)
}使用 sync.Mutex 互斥锁
go
// 需要使用互斥锁来保护 map 的并发访问。
type SafeCounter struct {
mu sync.RWMutex
v map[string]int
}
// 写锁保证同一时间只有一个 goroutine 可以访问 map。
func (c *SafeCounter) Inc(key string) {
c.mu.Lock()
c.v[key]++
c.mu.Unlock()
}
// 读锁可以同时被多个 goroutine 访问。
func (c *SafeCounter) Value(key string) int {
c.mu.RLock()
// Lock so only one goroutine at a time can access the map c.v.
defer c.mu.RUnlock()
return c.v[key]
}
func main() {
c := SafeCounter{v: make(map[string]int)}
for i := 0; i < 1000; i++ {
go c.Inc("somekey")
}
time.Sleep(time.Second)
fmt.Println(c.Value("somekey"))
}底层设计
线程到协程
- 线程是操作系统内核调度的最小单位,是程序执行的基本单位。其创建、销毁、调度都需要操作系统内核参与。
- 协程是处于用户态的调度单位,由用户态完成调度,无需操作系统内核参与。
Goroutine 也就是 Go 语言的协程实现。
