长安链 DApp 开发必学 Go 20:互斥锁

120 阅读1分钟

当使用协程并发处理数据时,绕不开的就是“互斥”的情况。

即多个协程需要对同一资源进行操作,这个时候就需要使用互斥锁。

Go 标准库提供了现成的互斥锁实现,直接拿来用。

上代码:

package main

import (
	"fmt"
	"sync"
	"time"
)

// SafeCounter is safe to use concurrently.
type SafeCounter struct {
	mu sync.Mutex                       // 属性中带一把锁
	v  map[string]int
}

// Inc increments the counter for the given key.
func (c *SafeCounter) Inc(key string) {
	c.mu.Lock()                           // 上锁,然后修改数据
	// Lock so only one goroutine at a time can access the map c.v.
	c.v[key]++
        c.mu.Unlock()                     // 别忘了释放锁
}

// Value returns the current value of the counter for the given key.
func (c *SafeCounter) Value(key string) int {
	c.mu.Lock()
	// Lock so only one goroutine at a time can access the map c.v.
	defer c.mu.Unlock()                  // 使用 defer,保证不会忘记释放锁
	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"))
}

锁的实际使用十分简单,Lock Unlock即可,注意不要死锁。

ok,下一章搞个协程爬虫练习,终于用到 Go 最擅长的 Web 领域了。