Thanakom

Sync.Mutex

Apr 18th, 2021
516
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package main
  2.  
  3. import (
  4.     "fmt"
  5. )
  6.  
  7. type SafeCounter struct {
  8.     v  map[string]int
  9. }
  10.  
  11. func (c *SafeCounter) Inc(key string) {
  12.     c.v[key]++
  13. }
  14.  
  15. func (c *SafeCounter) Value(key string) int {
  16.     return c.v[key]
  17. }
  18.  
  19. func main() {
  20.     c := SafeCounter{v: make(map[string]int)}
  21.     for i := 0; i < 1000; i++ {
  22.         go c.Inc("somekey")
  23.     }
  24.    
  25.     fmt.Println(c.Value("somekey"))
  26. }
  27.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×