Created
August 17, 2019 12:38
-
-
Save mhewedy/0e68bc4fab07ab13ddd47b0676d37b7c to your computer and use it in GitHub Desktop.
This file contains 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" | |
) | |
var cache = sync.Map{} | |
func main() { | |
var wg sync.WaitGroup | |
wg.Add(2) | |
go func() { | |
for i := 0; i < 1000; i++ { | |
if value, ok := cache.LoadOrStore("A", 1); ok { | |
cache.Store("A", value.(int)+1) | |
} | |
} | |
wg.Done() | |
}() | |
go func() { | |
for i := 0; i < 1000; i++ { | |
if value, ok := cache.LoadOrStore("B", 1); ok { | |
cache.Store("B", value.(int)+1) | |
} | |
} | |
wg.Done() | |
}() | |
wg.Wait() | |
fmt.Println(cache.Load("A")) | |
fmt.Println(cache.Load("B")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment