Skip to content

Instantly share code, notes, and snippets.

@sajal
Created December 8, 2016 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajal/50e173fcd3fa043f46e1e5084512d5de to your computer and use it in GitHub Desktop.
Save sajal/50e173fcd3fa043f46e1e5084512d5de to your computer and use it in GitHub Desktop.
map booboo
package main
import (
"sync"
"time"
)
// cache allows manipulating cached data.
type cache struct {
// Concurrent access control to maps
sync.RWMutex
ip map[string]string
}
func (c cache) spam() {
c.Lock()
defer c.Unlock()
c.ip["foo"] = "bar"
}
func main() {
c := cache{ sync.RWMutex{}, make(map[string]string) }
for i := 0; i < 10000; i++ {
go c.spam()
}
time.Sleep(time.Second)
}
package main
import (
"sync"
"time"
)
// cache allows manipulating cached data.
type cache struct {
// Concurrent access control to maps
sync.RWMutex
ip map[string]string
}
func (c cache) spam() {
c.Lock()
defer c.Unlock()
c.ip["foo"] = "bar"
}
func main() {
c := cache{
ip: make(map[string]string),
}
for i := 0; i < 10000; i++ {
go c.spam()
}
time.Sleep(time.Second)
}
package main
import (
"sync"
"time"
)
// cache allows manipulating cached data.
type cache struct {
// Concurrent access control to maps
*sync.RWMutex
ip map[string]string
}
func (c cache) spam() {
c.Lock()
defer c.Unlock()
c.ip["foo"] = "bar"
}
func main() {
c := cache{ &sync.RWMutex{}, make(map[string]string) }
for i := 0; i < 10000; i++ {
go c.spam()
}
time.Sleep(time.Second)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment