解决go语言concurrent map writes问题

本文作者:陈进坚
个人博客:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:[email protected]

运行下面的程序会报错fatal error: concurrent map writes

package main

import "time"

var m = make(map[int]int)
func main() {
   for i := 0; i < 10; i++ {
      go func(n int) {
         m[n]=n
      }(i)
   }
   time.Sleep(time.Duration(2)*time.Second)
}

原因是map为引用类型,高并发时对map并发写会产生竞争,不管是否同一个key都会报错,并发对map读是不会有问题的。

解决方法一

将map换成sync.Map

package main

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

var m sync.Map
func main() {
   for i := 0; i < 10; i++ {
      go func(n int) {
         m.Store("a",n)    //写入
      }(i)
   }
   time.Sleep(time.Duration(2)*time.Second)
   fmt.Println(m.Load("a"))   //读取
   //遍历
   m.Range(func(k, v interface{}) bool {
      fmt.Println( k, v)
      return true
   })
}

解决方法二

加锁

package main

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

var m = make(map[int]int)
var lock sync.Mutex

func main() {
   for i := 0; i < 10; i++ {
      go func(n int) {
         lock.Lock()       //加锁,加锁期间其他协程会进入阻塞状态直到解锁
         time.Sleep(time.Duration(2)*time.Second)
         fmt.Println(n,"在执行")
         m[n]=n
         lock.Unlock()  //解锁
      }(i)
   }
   time.Sleep(time.Duration(20)*time.Second)
}

你可能感兴趣的:(解决go语言concurrent map writes问题)