2024-09-21 15:51:58 +00:00
|
|
|
package counter
|
|
|
|
|
2024-09-21 17:43:09 +00:00
|
|
|
import "sync"
|
|
|
|
|
2024-09-21 15:51:58 +00:00
|
|
|
type Counter struct {
|
|
|
|
count int
|
2024-09-21 17:43:09 +00:00
|
|
|
mutex sync.Mutex
|
2024-09-21 15:51:58 +00:00
|
|
|
}
|
|
|
|
|
2024-09-21 17:48:58 +00:00
|
|
|
func NewCounter() *Counter {
|
|
|
|
return &Counter{}
|
|
|
|
}
|
|
|
|
|
2024-09-21 15:51:58 +00:00
|
|
|
func (c *Counter) Inc() {
|
2024-09-21 17:43:09 +00:00
|
|
|
c.mutex.Lock()
|
|
|
|
defer c.mutex.Unlock()
|
2024-09-21 15:51:58 +00:00
|
|
|
c.count++
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Counter) Value() int {
|
|
|
|
return c.count
|
|
|
|
}
|