当前位置:网站首页>Fatal error in golang: concurrent map writes
Fatal error in golang: concurrent map writes
2022-07-04 09:16:00 【The diligent bird that flies first】
The specific code is as follows :
package main
import (
"fmt"
"time"
)
var m = make(map[int]int, 10)
func solution(n int){
res := 1
for i:=1; i<=n; i++{
res = res * i
}
m[n] = res
}
func main(){
for i:=1; i<=200; i++{
go solution(i)
}
time.Sleep(time.Second*10)
for ind, val := range m{
fmt.Printf("[%d] = %d \n", ind, val)
}
}
The following error occurred :
fatal error: concurrent map writes
fatal error: concurrent map writes
runtime.mapassign_fast64(0x10b7760, 0xc00001e1b0, 0x12, 0x0)
/usr/local/go/src/runtime/map_fast64.go:176 +0x325 fp=0xc000106fa0 sp=0xc000106f60 pc=0x1010bc5
main.solution(0x12)
/Users/lcq/go/src/go_base/gochanneldemo/channeldemo.go:15 +0x65 fp=0xc000106fd8 sp=0xc000106fa0 pc=0x10a88a5
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1374 +0x1 fp=0xc000106fe0 sp=0xc000106fd8 pc=0x1062c41
created by main.main
/Users/lcq/go/src/go_base/gochanneldemo/channeldemo.go:20 +0x58
The main reason is that map Not thread safe , Therefore, it is not safe in the case of concurrency , Out of commission map.
Solution :
- Add lock
- Use sync.map
- Use channel( Multiple thread operations channel It's thread safe )
边栏推荐
- awk从入门到入土(9)循环语句
- 26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
- The map set type is stored in the form of key value pairs, and the iterative traversal is faster than the list set
- C语言-入门-基础-语法-[变量,常亮,作用域](五)
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
- Jianzhi offer 09 realizes queue with two stacks
- 《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
- 2022-2028 global tensile strain sensor industry research and trend analysis report
- Reading notes on how to connect the network - hubs, routers and routers (III)
- Mantis creates users without password options
猜你喜欢
](/img/3f/4d8f4c77d9fde5dd3f53ef890ecfa8.png)
C语言-入门-基础-语法-[运算符,类型转换](六)
](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C语言-入门-基础-语法-[主函数,头文件](二)

2022-2028 global gasket metal plate heat exchanger industry research and trend analysis report

Ehrlich sieve + Euler sieve + interval sieve

MySQL foundation 02 - installing MySQL in non docker version

到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了

2022-2028 global intelligent interactive tablet industry research and trend analysis report

Mantis creates users without password options

2022-2028 research and trend analysis report on the global edible essence industry

Implementation principle of redis string and sorted set
随机推荐
Multilingual Wikipedia website source code development part II
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
In depth research and investment strategy report on China's hydraulic parts industry (2022 Edition)
《网络是怎么样连接的》读书笔记 - FTTH
UML sequence diagram [easy to understand]
2022-2028 global edible probiotic raw material industry research and trend analysis report
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Service call feign of "micro service"
awk从入门到入土(9)循环语句
swatch
A subclass must use the super keyword to call the methods of its parent class
[error record] no matching function for call to 'cacheflush' cacheflush();)
Les différents modèles imbriqués de listview et Pageview avec les conseils de flutter
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
Nurse level JDEC addition, deletion, modification and inspection exercise
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Awk from entry to earth (15) awk executes external commands
C语言-入门-基础-语法-[运算符,类型转换](六)
Reading notes of how the network is connected - understanding the basic concepts of the network (I)