Skip to content

Instantly share code, notes, and snippets.

@vincent6767
Last active August 26, 2018 08:48
Show Gist options
  • Save vincent6767/3114871fa336004ebaa215803bc7a62b to your computer and use it in GitHub Desktop.
Save vincent6767/3114871fa336004ebaa215803bc7a62b to your computer and use it in GitHub Desktop.
Mutex: User to protect the state or several assets example
import (
“sync"
)
type Player struct {
sync.Mutex
Health int
}
func (p *Player) DecreaseHealthPoint(damageTaken int)
{
p.Lock() // lock access from other goroutine.
p.Health = p.Health - damageTaken // the protected shared resource
p.Unlock() // release the lock so other goroutine can access the struct.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment