Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@chowey
Created July 20, 2016 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chowey/bce83aeec640cfc013f3a69086a87c2b to your computer and use it in GitHub Desktop.
Save chowey/bce83aeec640cfc013f3a69086a87c2b to your computer and use it in GitHub Desktop.
package main
import "sync"
type ExitGroup struct {
wg sync.WaitGroup
cl []func()
lk sync.Mutex
}
func (e *ExitGroup) OnClose(fn func()) {
e.lk.Lock()
e.wg.Add(1)
e.cl = append(e.cl, fn)
e.lk.Unlock()
}
func (e *ExitGroup) OnCloseErr(fn func() error) {
e.OnClose(func() {
if err := fn(); err != nil {
log.Println(err)
}
})
}
func (e *ExitGroup) Exit() {
e.lk.Lock()
for _, fn := range e.cl {
fn()
e.wg.Done()
}
e.lk.Unlock()
}
func (e *ExitGroup) Wait() {
e.wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment