当前位置:网站首页>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 )
边栏推荐
- Report on the development trend and prospect trend of high purity zinc antimonide market in the world and China Ⓕ 2022 ~ 2027
- Dede plug-in (multi-function integration)
- Awk from digging into the ground to getting started (10) awk built-in functions
- awk从入门到入土(18)gawk线上手册
- Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
- 2022-2028 global seeder industry research and trend analysis report
- What is uid? What is auth? What is a verifier?
- 26. Delete duplicates in the ordered array (fast and slow pointer de duplication)
- 上周热点回顾(6.27-7.3)
- Solve the problem of "Chinese garbled MySQL fields"
猜你喜欢

2022-2028 global protein confectionery industry research and trend analysis report
](/img/5a/c6a3c5cd8038d17c5b0ead2ad52764.png)
C语言-入门-基础-语法-[主函数,头文件](二)

Jianzhi offer 09 realizes queue with two stacks

Codeforces Round #750 (Div. 2)(A,B,C,D,F1)

Sword finger offer 30 contains the stack of Min function

How to batch change file extensions in win10

Industry depression has the advantages of industry depression

GoLand environment variable configuration

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

Codeforces Round #803 (Div. 2)(A-D)
随机推荐
Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese PCB function test scale analysis and development prospect planning report Ⓑ 2022 ~ 2027
How to write unit test cases
2022-2028 global protein confectionery industry research and trend analysis report
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
Trees and graphs (traversal)
Awk from entry to penetration (6) regular matching
Launpad | 基础知识
Nurse level JDEC addition, deletion, modification and inspection exercise
Jianzhi offer 09 realizes queue with two stacks
Report on research and investment prospects of polyglycolic acid industry in China (2022 Edition)
awk从入门到入土(6)正则匹配
老掉牙的 synchronized 锁优化,一次给你讲清楚!
2022-2028 global industry research and trend analysis report on anterior segment and fundus OTC detectors
UML sequence diagram [easy to understand]
awk从入门到入土(12)awk也可以写脚本,替代shell
DR6018-CP01-wifi6-Qualcomm-IPQ6010-IPQ6018-FAMILY-2T2R-2.5G-ETH-port-CP01-802-11AX-MU-MIMO-OFDMA
Implementation principle of redis string and sorted set
ArrayBuffer
What exactly is DAAS data as a service? Don't be misled by other DAAS concepts