当前位置:网站首页>Concurrent writing of maps in golang
Concurrent writing of maps in golang
2022-06-24 14:33:00 【KunkkaWu】
principle
golang Medium map Not thread safe , Therefore, it cannot be used directly in the case of concurrency map.
Negative example
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
}
Runtime , Will report a mistake :
fatal error: concurrent map writes
terms of settlement
1. Map Lock
package main
import (
"fmt"
"strconv"
"sync"
"time"
)
// Definite lock
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()
}
- Use 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)
}
}()
}
边栏推荐
- IList of PostgreSQL
- Explore cloud native databases and take a broad view of future technological development
- 【Pytorch】量化
- box-sizing
- Defeat the binary tree!
- IDEA连接mysql自定义生成实体类代码
- `Thymeleaf`模板引擎全面解析
- 兴业证券靠谱吗?开证券账户安全吗?
- Online text entity extraction capability helps applications analyze massive text data
- Go language -init() function - package initialization
猜你喜欢
From pair to unordered_ Map, theory +leetcode topic practice
Bert-whitening 向量降维及使用
Go language - use of goroutine coroutine
Qunhui synchronizes with alicloud OSS
laravel8使用faker调用工厂填充数据
box-sizing
常见的单例模式&简单工厂
ASCII code table extracted from tanhaoqiang's C program design (comparison table of common characters and ASCII codes)
测试 H5 和小程序的区别,你真的知道吗?
tongweb使用之端口冲突处理办法
随机推荐
Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
同样是初级测试工程师,为啥他薪资高?会这几点面试必定出彩
Go language -init() function - package initialization
Common singleton mode & simple factory
时间同步业务的闭环管理——时间监测
R语言构建回归模型诊断(正态性无效)、进行变量变换、使用car包中的powerTransform函数对目标变量进行Box-Cox变换(Box–Cox transform to normality)
10_那些格调很高的个性签名
Preliminary study on AQS
Database considerations
一个简单而功能强大的开发者工具箱Box3.cc
Esp32 series -- comparison of esp32 series
[untitled]
ES mapping之keyword;term查询添加keyword查询;更改mapping keyword类型
Method of establishing unity thermodynamic diagram
June training (day 24) - segment tree
[environment setup] zip volume compression
第八章 操作位和位串(四)
不要小看了积分商城,它的作用可以很大
中国十大证券app排名 炒股开户安全吗
Keras深度学习实战(11)——可视化神经网络中间层输出