You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

24 lines
435 B
Go

package my_sync
4 months ago
import "sync"
type Counter struct {
4 months ago
// mutual exclusion lock. Zero value is an unlocked Mutex
// If we embedded the type it would be c.Lock() but that also makes all other
// Mutex functions part of the public interface, which we don't want.
// sync.Mutex
mu sync.Mutex
count int
}
func (c *Counter) Inc() {
4 months ago
c.mu.Lock()
defer c.mu.Unlock()
c.count++
}
func (c *Counter) Value() int {
return c.count
}