Last active
July 12, 2018 13:10
-
-
Save arpchaudhary/209219949c9b5c217e103e0bc4affce2 to your computer and use it in GitHub Desktop.
Concurrent Hash Map in Go
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 ( | |
PROC_MAP = struct { | |
sync.RWMutex | |
m map[string]bool | |
}{m: make(map[string]bool)} | |
) | |
func record(data string) { | |
PROC_MAP.Lock() | |
PROC_MAP.m[data] = true | |
PROC_MAP.Unlock() | |
} | |
func verify(data string) bool { | |
PROC_MAP.RLock() | |
status := PROC_MAP.m[data] | |
PROC_MAP.RUnlock() | |
return status | |
} | |
func main() { | |
record("bar") | |
record("foo") | |
done := make(chan bool) | |
go func() { | |
fmt.Println("Verify [bar]", verify("bar")) | |
fmt.Println("Verify [foobar]", verify("foobar")) | |
fmt.Println("Verify [foo]", verify("foo")) | |
done <- true | |
}() | |
<-done | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment