go map fatal error:concurrent map read and map write
2017-05-26 22:29
603 查看
go map fatal error:concurrent map read and map write
golang中map并发读写问题及解决方法
go语言切片slice的线程协程安全问题
一、map并发读写问题如果map由多协程同时读和写就会出现 fatal error:concurrent map read and map write的错误
如下代码很容易就出现map并发读写问题
func main(){
c := make(map[string]int)
Go func() {//开一个协程写map
for j := 0; j < 1000000; j++ {
c[fmt.Sprintf("%d", j)] = j
}
}()
go func() { //开一个协程读map
for j := 0; j < 1000000; j++ {
fmt.Println(c[fmt.Sprintf("%d",j)])
}
}()
time.Sleep(time.Second*20)
}
多个协程同时写也会出现fatal error: concurrent map writes的错误
如下代码很容易出现map并发写的问题
func main(){
c := make(map[string]int)
for i := 0; i < 100; i++ {
go func() { //开100个协程并发写map
for j := 0; j < 1000000; j++ {
c[fmt.Sprintf("%d", j)] = j
}
}()
}
time.Sleep(time.Second*20) //让执行main函数的主协成等待20s,不然不会执行上面的并发操作
}
二、出现问题的原因
因为map为引用类型,所以即使函数传值调用,参数副本依然指向映射m, 所以多个goroutine并发写同一个映射m, 写过多线程程序的同学都知道,对于共享变量,资源,并发读写会产生竞争的, 故共享资源遭到破坏
三、解决方法
1、加锁
(1)通用锁
type Demo struct {
Data map[string]string
Lock sync.Mutex
}
func (d Demo) Get(k string) string{
d.Lock.Lock()
defer d.Lock.UnLock()
return d.Data[k]
}
func (d Demo) Set(k,v string) {
d.Lock.Lock()
defer d.Lock.UnLock()
d.Data[k]=v
}
(2)读写锁
type MapTplResCode
struct {
Data map[string]int
Lock *sync.RWMutex
}
func (d MapTplResCode)
Get(k string) (int,
bool) {
d.Lock.RLock()
defer d.Lock.RUnlock()
if v,
ok := d.Data[k]; ok {
return v,
true
}
return 0,
false
}
func (d MapTplResCode)
Set(k string, v
int) {
d.Lock.Lock()
defer d.Lock.Unlock()
d.Data[k] = v
}
func (d MapTplResCode)
Init() {
d.Lock.Lock()
defer d.Lock.Unlock()
//d.Data = make(map[string]int)
for key,
_ := range d.Data {
delete(d.Data, key)
}
}
2、利用channel串行化处理
相关文章推荐
- golang map引发的 【fatal error: concurrent map read and map write】问题解决
- fatal error: concurrent map read and map write
- unity3d fatal error attempt to write a readonly database解决方法
- file read and write use java
- HOOK api readprocessmemory and writeprocessmemory
- C# modify file (read all and modify and write all)
- DM642使用ccs3.1时GEL的问题:GEL: Error calling OnPreFileLoaded(): memory map prevented write to target memory at 0x01800054
- Read and write flat file
- Configure file like format of ini and read data from ini file or write data to ini file
- How To Read and Write BLOB Data by Using ADO.NET with Visual C# .NET
- Create, Read, Write, Copy, Move and Delete a Text File using C#
- Read and Write XML
- What is SPAWN concurrent program and how to write such program?
- [zz] Read and write a properties file
- Readonly and Mutable, vs. Read / Write Properties
- Infopath XML read and write
- read and write excel file as a table
- Java libraries to read and write PDF files
- WriteAndReadFile
- CSpreadSheet - A Class to Read and Write to Excel and Text Delimited Spreadsheet