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
24 lines
435 B
Go
package my_sync
|
|
|
|
import "sync"
|
|
|
|
type Counter struct {
|
|
// 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() {
|
|
c.mu.Lock()
|
|
defer c.mu.Unlock()
|
|
c.count++
|
|
}
|
|
|
|
func (c *Counter) Value() int {
|
|
return c.count
|
|
}
|