当前位置:网站首页>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 )
边栏推荐
- 2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
- Horizon sunrise X3 PI (I) first boot details
- C语言-入门-基础-语法-[主函数,头文件](二)
- Awk from entry to soil (5) simple condition matching
- Service call feign of "micro service"
- Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
- Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
- At the age of 30, I changed to Hongmeng with a high salary because I did these three things
- If you can quickly generate a dictionary from two lists
- Global and Chinese markets for laser assisted liposuction (LAL) devices 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
2022-2028 global visual quality analyzer industry research and trend analysis report
Mantis creates users without password options
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
Langage C - démarrer - base - syntaxe - [opérateur, conversion de type] (vi)
2022-2028 global small batch batch batch furnace industry research and trend analysis report
Opencv environment construction (I)
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Ehrlich sieve + Euler sieve + interval sieve
随机事件的关系与运算
[untitled] forwarding least square method
随机推荐
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
Target detection -- intensive reading of yolov3 paper
C语言-入门-基础-语法-数据类型(四)
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
Explain TCP protocol in detail three handshakes and four waves
What is permission? What is a role? What are users?
The old-fashioned synchronized lock optimization will make it clear to you at once!
Dede plug-in (multi-function integration)
如何编写单元测试用例
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
China electronic grade sulfur trioxide Market Forecast and investment strategy report (2022 Edition)
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
Reading notes on how to connect the network - hubs, routers and routers (III)
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
awk从入门到入土(11)awk getline函数详解
上周热点回顾(6.27-7.3)
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
什么是uid?什么是Auth?什么是验证器?