当前位置:网站首页>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)
}
}()
}边栏推荐
- In the eyes of the universe, how to correctly care about counting East and West?
- [learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
- R language plot visualization: use plot to visualize the training set and test set after data division, use different shape label representation, training set, test set, and display training and test
- R语言plotly可视化:使用plotly可视化数据划分后的训练集和测试集、使用不同的形状标签表征、训练集、测试集、以及数据集的分类标签(Display training and test split
- STM32F1与STM32CubeIDE编程实例-WS2812B全彩LED驱动(基于SPI+DMA)
- tongweb使用之端口冲突处理办法
- One article to get UDP and TCP high-frequency interview questions!
- ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型
- ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
- Go语言三个高效编程的技巧
猜你喜欢

Linux 安装 CenOS7 MySQL - 8.0.26

ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型

MySQL日志管理、备份与恢复

数字臧品系统开发 NFT数字臧品系统异常处理源码分享
![[deep learning] storage form of nchw, nhwc and chwn format data](/img/4f/4478d96132eb2547f6ec09ae49639e.jpg)
[deep learning] storage form of nchw, nhwc and chwn format data

Data sharing between laravel lower views

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

Three efficient programming skills of go language

Overview of SAP marketing cloud functions (III)

Port conflict handling method for tongweb
随机推荐
Go language -init() function - package initialization
【ansible问题处理】远程执行用户环境变量加载问题
IDEA连接mysql自定义生成实体类代码
不要小看了积分商城,它的作用可以很大
Linux Installation cenos7 MySQL - 8.0.26
Application of motion capture system in positioning and mapping of mobile robot in underground tunnel
postgresql之词法分析简介
一文搞定 UDP 和 TCP 高频面试题!
Chapter 8 operation bit and bit string (4)
GO语言-goroutine协程的使用
Alibaba OSS object storage service
Bert whitening vector dimension reduction and its application
二叉树中最大路径和[处理好任意一颗子树,就处理好了整个树]
Method of establishing unity thermodynamic diagram
Three efficient programming skills of go language
09_ An efficient memory method
高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
[untitled]
The function and principle of key in V-for
R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签