golang多个routine操作map或者slice需要上锁
2018-02-15 23:10
441 查看
最近写服务器代码,运行期间突然panic,最后找到原因是因为map不是线程安全的,那这就简单啦,来一把读写锁。以下附上代码:
type QueryExchangerFlowBySomeTime struct { com.HandlerDemo}func (self *QueryExchangerFlowBySomeTime) FuckBody(body interface{}) (result interface{}, err error) { obj := *body.(*map[string]interface{}) ip:=to.String(obj["ip"]) granule:=to.Int64(obj["granule"]) log.Println("传入:",granule) retMap:=make(map[string]interface{}) retMap["starttime"]=GetHisTime() ips:=GetAllMac(ip) retData:=ReqDataHub{} time_out:=make(chan int,2) go ReqTimeOut(time_out,1,DATAHUB_REQ_TIME) var wg sync.WaitGroup for _,v:=range ips{ wg.Add(1) go func(v string,g int64,ipt string){ retData.Lock() retData.Data[v]=GetDataHubDataByIp(v,g,ipt) retData.Unlock() wg.Done() }(v,granule,ip) } select{ case <-time_out: return com.Result{ Status:"209", Msg:"请求超时", },nil default: wg.Wait()
} retMap["data"]=retData return com.Result{ Status:"200", Data:retMap, Msg:"成功", },nil}func (self *QueryExchangerFlowBySomeTime) CheckBody(body interface{}) error { obj := *body.(*map[string]interface{}) if err := com.HasValues(obj,"granule","ip"); err != nil { return err } return nil}
golang的锁是不可重入的锁,就是说成双成对的出现,其实关键的部分就在操作map的前后加上锁,, retData.Lock() 和redData.Unlock(),FuckBody是封装的一套业务处理函数
type QueryExchangerFlowBySomeTime struct { com.HandlerDemo}func (self *QueryExchangerFlowBySomeTime) FuckBody(body interface{}) (result interface{}, err error) { obj := *body.(*map[string]interface{}) ip:=to.String(obj["ip"]) granule:=to.Int64(obj["granule"]) log.Println("传入:",granule) retMap:=make(map[string]interface{}) retMap["starttime"]=GetHisTime() ips:=GetAllMac(ip) retData:=ReqDataHub{} time_out:=make(chan int,2) go ReqTimeOut(time_out,1,DATAHUB_REQ_TIME) var wg sync.WaitGroup for _,v:=range ips{ wg.Add(1) go func(v string,g int64,ipt string){ retData.Lock() retData.Data[v]=GetDataHubDataByIp(v,g,ipt) retData.Unlock() wg.Done() }(v,granule,ip) } select{ case <-time_out: return com.Result{ Status:"209", Msg:"请求超时", },nil default: wg.Wait()
} retMap["data"]=retData return com.Result{ Status:"200", Data:retMap, Msg:"成功", },nil}func (self *QueryExchangerFlowBySomeTime) CheckBody(body interface{}) error { obj := *body.(*map[string]interface{}) if err := com.HasValues(obj,"granule","ip"); err != nil { return err } return nil}
golang的锁是不可重入的锁,就是说成双成对的出现,其实关键的部分就在操作map的前后加上锁,, retData.Lock() 和redData.Unlock(),FuckBody是封装的一套业务处理函数
相关文章推荐
- golang学习之slice基本操作
- 我的电脑系统有问题中毒了,或者有需要还原系统该怎么操作?
- 【Go入门教程4】变量(var),常量(const),内置基础类型(Boolean、数值 byte,int,rune、字符串、错误类型),分组,iota枚举,array(数值),slice(切片),map(字典),make/new操作,零值
- Go语言基础:array、slice、make和new操作、map
- golang中slice切片的修改操作
- list,set等集合遍历时,不能remove集合中的元素。需要new一个Object或者list,set,里面add需要删除的元素,等集合遍历完了进行remove(Object)或者removeAll(list/set)操作
- 【玩转Golang】slice切片的操作——切片的追加、删除、插入等
- GOLANG笔记-04-ARRAY/SLICE/MAP
- Mybatis中传入值,只传入list集合或者数组或者map操作
- golang之slice、array、map
- Json string 递归成 Map 集合操作,或者直接递归操作
- 对GoLang里的slice要谨慎使用append操作
- Golang学习笔记(四):array、slice、map
- 数据库出现问号,界面能正常显示,如果数据的数据需要备份或者导出的操作
- 深入理解golang — 数组(array)、切片(slice)、map
- Golang 学习之路七:数据(Array、Slice、Map、Struct)
- golang 中map并发读写操作
- 操作链表,需要记住保存前一个结点,以及记录下一个节点,否则容易进入死循环,或者数据出错。
- golang中对slice操作工具类
- 【玩转Golang】slice切片的操作——切片的追加、删除、插入等