33 lines
424 B
Go
33 lines
424 B
Go
package main
|
|
|
|
import (
|
|
"strconv"
|
|
"sync"
|
|
)
|
|
|
|
type Counter struct {
|
|
count int
|
|
lock sync.Mutex
|
|
}
|
|
|
|
func NewCounter() *Counter {
|
|
return &Counter{count: 0}
|
|
}
|
|
|
|
func (c *Counter) Add() {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
c.count += 1
|
|
}
|
|
|
|
func (c *Counter) Sub() {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
c.count -= 1
|
|
}
|
|
|
|
func (c *Counter) String() string {
|
|
c.lock.Lock()
|
|
defer c.lock.Unlock()
|
|
return strconv.Itoa(c.count)
|
|
} |