zoukankan      html  css  js  c++  java
  • golang在多个go routine中进行map或者slice操作应该注意的对象。

    因为golang的map和列表切片都是引用类型,且非线程安全的,所以在多个go routine中进行读写操作的时候,会产生“map read and map write“的panic错误。

    某一些类型的对象,会有这种类似的set方法来写数据,或者get方法来返回一个map:

    func (this *object) Set(name, val) {
      this.Lock()
      defer this.Unlock()
      
      this.m[name] = val
    }
    
    func (this *object) Get() map[string]string {
      this.Lock()
      defer this.Unlock()
    
      return this.m
    }
    

      

    如果会在多个go routine中通过该对象的Get()方法获取到的map进行读操作,并且在其他go routine中用Set()方法来写操作,那么有可能会导致“map read and map write“的panic错误。

    原因是Get方法获取的map和Set方法操作的map是同一个map,如果读写线程在同一时刻操作这2个map,就会产生错误。

    所以Get方法最好用这种方式返回map:

    func (this *object) Get() map[string]string {
        this.Lock()
        defer this.Unlock()
    
        newm := make(map[string]string)
        for k, v := range this.m {
            newm[k] = v
        }
    
        return newm
    }
    

    这样每次Get获取的map,其实是一个新的map,就可以不用考虑同时读写的问题了。

  • 相关阅读:
    时间序列模型文章收集
    因果推断文章收集
    Git常用命令
    redis配置
    团队作业2:需求分析&原型设计
    团队项目作业1-团队展示与选题
    结对编程1-模块化
    个人作业2:APP案例分析
    为农三载
    面试题随记一
  • 原文地址:https://www.cnblogs.com/yzhch/p/8137128.html
Copyright © 2011-2022 走看看