Last active
February 8, 2022 00:04
-
-
Save eternnoir/2cae091ab5862835bfcfa86bc6f9d2de to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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