golang is 1.9.2
recently contacted with the concurrent locking function of golang, I did a multi-goroutine counting exercise according to the example, and found that the results of locking and unlocking are the same. Is this because of the machine? (at least on my computer and on https://play.golang.org/) or does version 1.9 of go no longer use mutex lock data? If it"s my test method, how can I test the difference between locked and unlocked in the right way?
the code is as follows:
package main
import (
"fmt"
"sync"
"time"
)
type SafeCounter struct {
v map[string]int
mux sync.Mutex
}
func (c *SafeCounter) Inc(key string) {
c.mux.Lock()
c.v[key]PP
c.mux.Unlock()
}
func (c *SafeCounter) Value(key string) int {
defer c.mux.Unlock()
c.mux.Lock()
return c.v[key]
}
func main() {
c := SafeCounter{
v: make(map[string]int),
}
for i := 0; i < 1000; iPP {
go c.Inc("somekey")
}
time.Sleep(time.Second)
fmt.Println(c.Value("somekey"))
}
output 1000
remove all locks, or 1000