Created
January 16, 2024 13:15
-
-
Save anativ/6702b1956d4a12065b2be13b2e3973b7 to your computer and use it in GitHub Desktop.
go copy on write map
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
func main() { | |
m := map[string]int{} | |
for i := 0; i < 1000; i++ { | |
m[fmt.Sprintf("key%d", i)] = i | |
} | |
go func() { | |
for { | |
m1 := make(map[string]int) | |
for k, v := range m { | |
m1[k] = v | |
} | |
m1[time.Now().String()] = 1 | |
m = m1 | |
} | |
}() | |
wg := sync.WaitGroup{} | |
for i := 0; i < 100; i++ { | |
wg.Add(1) | |
go func(wg *sync.WaitGroup, id int) { | |
defer wg.Done() | |
s := "" | |
val := 0 | |
index := 0 | |
for { | |
for k, v := range m { | |
s = k | |
val += v | |
index++ | |
if index%10000 == 0 { | |
fmt.Println(id, len(m), index, s) | |
} | |
} | |
} | |
}(&wg, i) | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment