当前位置:网站首页>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 )
边栏推荐
- [error record] no matching function for call to 'cacheflush' cacheflush();)
- Awk from entry to penetration (6) regular matching
- Awk from getting started to digging in (11) detailed explanation of awk getline function
- Global and Chinese market of planar waveguide optical splitter 2022-2028: Research Report on technology, participants, trends, market size and share
- C語言-入門-基礎-語法-[運算符,類型轉換](六)
- Trim leading or trailing characters from strings- Trim leading or trailing characters from a string?
- Implementation principle of redis string and sorted set
- 20220701 Barbalat引理证明
- Global and Chinese market of sampler 2022-2028: Research Report on technology, participants, trends, market size and share
- Dynamic analysis and development prospect prediction report of high purity manganese dioxide in the world and China Ⓡ 2022 ~ 2027
猜你喜欢
到底什么才是DaaS数据即服务?别再被其他DaaS概念给误导了
C语言-入门-基础-语法-数据类型(四)
2022-2028 global special starch industry research and trend analysis report
AMLOGIC gsensor debugging
2022-2028 global gasket plate heat exchanger industry research and trend analysis report
Opencv environment construction (I)
You can see the employment prospects of PMP project management
2022-2028 global visual quality analyzer industry research and trend analysis report
GoLand environment variable configuration
C language - Introduction - Foundation - syntax - data type (4)
随机推荐
UML 时序图[通俗易懂]
swatch
PMP registration process and precautions
Flutter 小技巧之 ListView 和 PageView 的各種花式嵌套
LeetCode 74. Search 2D matrix
Use Alibaba cloud NPM image acceleration
什么是uid?什么是Auth?什么是验证器?
LinkedList in the list set is stored in order
Awk from entry to soil (5) simple condition matching
China battery grade manganese sulfate Market Forecast and strategic consulting report (2022 Edition)
The old-fashioned synchronized lock optimization will make it clear to you at once!
"How to connect the network" reading notes - Web server request and response (4)
C language - Introduction - Foundation - syntax - [variable, constant light, scope] (V)
What is uid? What is auth? What is a verifier?
《网络是怎么样连接的》读书笔记 - Tcp/IP连接(二)
Implementation principle of redis string and sorted set
Global and Chinese market of wheel hubs 2022-2028: Research Report on technology, participants, trends, market size and share
Report on research and investment prospect prediction of China's electronic grade sulfuric acid industry (2022 Edition)
HMS core helps baby bus show high-quality children's digital content to global developers
Function comparison between cs5261 and ag9310 demoboard test board | cost advantage of cs5261 replacing ange ag9310