当前位置:网站首页>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 )
边栏推荐
- Analysis report on the development status and investment planning of China's modular power supply industry Ⓠ 2022 ~ 2028
- 2022-2028 global strain gauge pressure sensor industry research and trend analysis report
- Awk from digging into the ground to getting started (10) awk built-in functions
- Target detection -- intensive reading of yolov3 paper
- 2022-2028 global probiotics industry research and trend analysis report
- 1211 or chicken and rabbit in the same cage
- C语言-入门-基础-语法-[运算符,类型转换](六)
- 什么是uid?什么是Auth?什么是验证器?
- Live in a dream, only do things you don't say
- awk从入门到入土(11)awk getline函数详解
猜你喜欢

HMS core helps baby bus show high-quality children's digital content to global developers

You can see the employment prospects of PMP project management

What exactly is DAAS data as a service? Don't be misled by other DAAS concepts

Nurse level JDEC addition, deletion, modification and inspection exercise

2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors

After unplugging the network cable, does the original TCP connection still exist?

How to batch change file extensions in win10

Live in a dream, only do things you don't say

Relationship and operation of random events

ArrayBuffer
随机推荐
什么是uid?什么是Auth?什么是验证器?
AMLOGIC gsensor debugging
Global and Chinese market of air fryer 2022-2028: Research Report on technology, participants, trends, market size and share
Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
C语言-入门-基础-语法-[主函数,头文件](二)
Research Report on research and investment prospects of China's testing machine industry (2022 Edition)
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
Awk from getting started to digging in (9) circular statement
Awk from entry to earth (7) conditional statements
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
Awk from entry to earth (8) array
Implementation principle of redis string and sorted set
Awk from entry to penetration (6) regular matching
Codeforces Round #750 (Div. 2)(A,B,C,D,F1)
上周热点回顾(6.27-7.3)
Horizon sunrise X3 PI (I) first boot details
Reload CUDA and cudnn (for tensorflow and pytorch) [personal sorting summary]
Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310
Global and Chinese markets of thrombography hemostasis analyzer (TEG) 2022-2028: Research Report on technology, participants, trends, market size and share