Last active
August 26, 2018 08:48
-
-
Save vincent6767/3114871fa336004ebaa215803bc7a62b to your computer and use it in GitHub Desktop.
Mutex: User to protect the state or several assets example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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