当前位置:网站首页>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 writesterms 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)
}
}()
}边栏推荐
- Second, the examinee must see | consolidate the preferred question bank to help the examinee make the final dash
- As a developer, what is the most influential book for you?
- Laravel8 uses faker to call factory to fill data
- R语言plotly可视化:可视化模型在整个数据空间的分类轮廓线(等高线)、meshgrid创建一个网格,其中每个点之间的距离由mesh_size变量表示、使用不同的形状标签表征、训练、测试及分类标签
- 【LeetCode】10、正则表达式匹配
- A common defect management tool - Zen, which teaches you from installation to using the handle
- PgSQL queries the largest or smallest data of a field in a group
- Daily knowledge popularization
- R语言plotly可视化:使用plotly可视化数据划分后的训练集和测试集、使用不同的形状标签表征、训练集、测试集、以及数据集的分类标签(Display training and test split
- Common singleton mode & simple factory
猜你喜欢

从pair到unordered_map,理论+leetcode题目实战

不要小看了积分商城,它的作用可以很大

As a developer, what is the most influential book for you?

From pair to unordered_ Map, theory +leetcode topic practice

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

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

leetcode. 12 --- integer to Roman numeral

Mots clés pour la cartographie es; Ajouter une requête par mot - clé à la requête term; Changer le type de mot - clé de cartographie

API data interface for announcement of Hong Kong listed companies

Idea connection MySQL custom generated entity class code
随机推荐
实战 | 记一次曲折的钓鱼溯源反制
六月集训(第24天) —— 线段树
10_那些格調很高的個性簽名
Linux Installation cenos7 MySQL - 8.0.26
IDEA 插件 Material Theme UI收费后的办法
Keras深度学习实战(11)——可视化神经网络中间层输出
在宇宙的眼眸下,如何正确地关心东数西算?
postgresql之词法分析简介
Py's toad: a detailed introduction to toad, its installation and use
pgsql查询分组中某个字段最大或者最小的一条数据
Esp32 series -- comparison of esp32 series
leetcode. 12 --- integer to Roman numeral
leetcode:1504. Count the number of all 1 sub rectangles
09_一种比较高效的记忆方法
MySQL log management, backup and recovery
API data interface for announcement of Hong Kong listed companies
中国十大证券app排名 炒股开户安全吗
Successfully solved: selenium common. exceptions. SessionNotCreatedException: Message: session not created: This versi
股票开户要找谁?在线开户安全么?
[ansible problem processing] remote execution user environment variable loading problem