Created
December 8, 2016 10:28
-
-
Save sajal/50e173fcd3fa043f46e1e5084512d5de to your computer and use it in GitHub Desktop.
map booboo
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 ( | |
"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) | |
} |
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 ( | |
"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) | |
} |
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 ( | |
"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