Skip to content

Instantly share code, notes, and snippets.

@zwh8800
Created June 16, 2016 07:02
Show Gist options
  • Save zwh8800/91b94bd54eb6574785002764bb462af5 to your computer and use it in GitHub Desktop.
Save zwh8800/91b94bd54eb6574785002764bb462af5 to your computer and use it in GitHub Desktop.
读写锁不能这么用。。。
func (bus *EventBus) find(register *Register) int {
bus.registersLock.RLock()
defer bus.registersLock.RUnlock()
for i := 0; i < len(bus.registers); i++ {
if bus.registers[i] == register {
return i
}
}
return -1
}
func (bus *EventBus) remove(i int) {
bus.registersLock.Lock()
defer bus.registersLock.Unlock()
bus.registers = append(bus.registers[:i], bus.registers[i+1:]...)
}
func (bus *EventBus) Remove(register *Register) {
i := bus.find(register)
if i != -1 {
bus.remove(i)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment