mirror of
https://github.com/alrayyes/wiki.git
synced 2024-11-21 19:16:23 +00:00
664 B
664 B
date | id | title |
---|---|---|
2020-09-18 | 56a802e2-3871-4746-aec4-fa4bdae4d4fd | Golang Mutex |
Description
Mutex allows up to add locks to
our data so it can be accessed safely in a concurrent manner. While
locked other threads can't access the data. Mutexes
should generally
be used for managing
state.
Syntax
type Counter struct {
mu sync.Mutex
value int
}
func (c *Counter) Inc() {
c.mu.Lock()
defer c.mu.Unlock()
c.value++
}
func (c *Counter) Value() int {
return c.value
}
func NewCounter() *Counter {
return &Counter{}
}