package main
import (
"fmt"
"sync"
)
type Counter struct {
value int
mutex sync.Mutex
}
func (c *Counter) Increment() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.value++
}
func (c *Counter) Decrement() {
c.mutex.Lock()
defer c.mutex.Unlock()
c.value--
}
func (c *Counter) GetValue() int {
c.mutex.Lock()
defer c.mutex.Unlock()
return c.value
}
func main() {
counter := Counter{}
var wg sync.WaitGroup
for i := 0; i < 1000; i++ {
wg.Add(1)
go func() {
defer func() { wg.Done() }()
counter.Increment()
}()
}
wg.Wait()
fmt.Println(counter.GetValue())
}