当前位置:网站首页>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)
}
}()
}
边栏推荐
- Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
- 3 ring kill 360 security guard process
- ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型
- Qunhui synchronizes with alicloud OSS
- [learn ZABBIX from scratch] I. Introduction and deployment of ZABBIX
- 10_那些格調很高的個性簽名
- 【Pytorch】量化
- 高薪程序员&面试题精讲系列115之Redis缓存如何实现?怎么发现热key?缓存时可能存在哪些问题?
- Development of digital Tibetan product system NFT digital Tibetan product system exception handling source code sharing
- MySQL日志管理、备份与恢复
猜你喜欢
随机推荐
在宇宙的眼眸下,如何正确地关心东数西算?
10_那些格调很高的个性签名
`Thymeleaf ` template engine comprehensive analysis
入行 4 年,跳槽 2 次,我摸透了软件测试这一行
Laravel8 uses faker to call factory to fill data
The "little giant" specialized in special new products is restarted, and the "enterprise cloud" digital empowerment
从pair到unordered_map,理论+leetcode题目实战
June training (day 24) - segment tree
Convolution kernel and characteristic graph visualization
How to avoid placing duplicate orders
GO语言并发模型-MPG模型
Kunpeng arm server compilation and installation paddlepaddle
GO语言-goroutine协程的使用
如何避免下重复订单
MySQL log management, backup and recovery
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
常见的缺陷管理工具——禅道,从安装到使用手把手教会你
50 practical applications of R language (23) - important concepts of Bayesian Theory: credibility, model models, and parameters
港股上市公司公告 API 数据接口
laravel 8 实现Auth登录