当前位置:网站首页>golang中Map的并发写入
golang中Map的并发写入
2022-06-24 14:29:00 【KunkkaWu】
原理
golang中的map不是线程安全的,所以在并发的情况下不能直接使用map。
反面例子
import (
"strconv"
"time"
)
var m = make(map[string]interface{})
func main() {
testMap(m)
time.Sleep(2 * time.Second)
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
writeMap(m, name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
writeMap(m, name, i)
}
}()
}
func writeMap(m map[string]int, key string, val int) {
m[key] = val
}运行时,会报错:
fatal error: concurrent map writes解决办法
1. Map加锁
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
// 定义锁
var locker = &sync.RWMutex{}
var m = make(map[string]interface{})
func main() {
testMap()
time.Sleep(2 * time.Second)
fmt.Println(m)
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
writeMap(name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
writeMap(name, i)
}
}()
}
func writeMap(key string, val int) {
locker.Lock()
m[key] = val
locker.Unlock()
}- 使用sync.Map
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
var m = sync.Map{}
func main() {
testMap()
time.Sleep(2 * time.Second)
m.Range(func(k, v interface{}) bool {
fmt.Println("map:", k, v)
return true
})
}
func testMap() {
go func() {
for i := 0; i < 10; i++ {
name := "WangWu" + strconv.Itoa(i)
m.Store(name, i)
}
}()
go func() {
for i := 0; i < 10; i++ {
name := "ZhuoQi" + strconv.Itoa(i)
m.Store(name, i)
}
}()
}边栏推荐
- R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
- 高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
- Development of B2B transaction collaborative management platform for kitchen and bathroom electrical appliance industry and optimization of enterprise inventory structure
- SSH keygen configuration does not require entering a password every time
- A review of text contrastive learning
- Three efficient programming skills of go language
- SaaS management system solution of smart Park: enabling the park to realize information and digital management
- leetcode 139. Word Break 单词拆分(中等)
- 日常知识科普
- 【LeetCode】10、正则表达式匹配
猜你喜欢

GO语言-goroutine协程的使用
![[bitbear story collection] June MVP hero story | technology practice collision realm thinking](/img/b7/ca2f8cfb124e7c68da0293624911d1.png)
[bitbear story collection] June MVP hero story | technology practice collision realm thinking

Kunpeng arm server compilation and installation paddlepaddle

c语言---18 函数(自定义函数)

Idea connection MySQL custom generated entity class code

ES mapping之keyword;term查詢添加keyword查詢;更改mapping keyword類型

tongweb使用之端口冲突处理办法

同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩

box-sizing

ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
随机推荐
根据前序&中序遍历生成二叉树[左子树|根|右子树的划分/生成/拼接问题]
3环杀掉360安全卫士进程
【环境搭建】zip 分卷压缩
c语言---18 函数(自定义函数)
Go language - use of goroutine coroutine
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
ESP32系列--ESP32各个系列对比
Explore cloud native databases and take a broad view of future technological development
The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
Some basic database operations (providing the original database information)
入行 4 年,跳槽 2 次,我摸透了软件测试这一行
同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩
Overview of SAP marketing cloud functions (IV)
June training (day 24) - segment tree
Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
leetcode 139. Word Break 单词拆分(中等)
测试 H5 和小程序的区别,你真的知道吗?
Keras深度学习实战(11)——可视化神经网络中间层输出
数据库一些基本操作(提供了原数据库信息)
MySQL title