Skip to content

Instantly share code, notes, and snippets.

@eternnoir
Last active February 8, 2022 00:04
Show Gist options
  • Save eternnoir/2cae091ab5862835bfcfa86bc6f9d2de to your computer and use it in GitHub Desktop.
Save eternnoir/2cae091ab5862835bfcfa86bc6f9d2de to your computer and use it in GitHub Desktop.
func GodDoSomething(ctx Contex){
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
ds1ctx ,ds1CancelFunc := context.WithCancel(ctx)
wg.Add(1)
go DoSomething1(ds1ctx, wg)
ds2ctx ,ds2CancelFunc := context.WithCancel(ctx)
wg.Add(1)
go DoSomething2(ds2ctx, wg)
select {
case <-c:
// Complete normally
return
case ctx.Done():
// Cancel by who call you
ds1CancelFunc()
ds2CancelFunc()
return
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment