Created
January 5, 2014 07:19
-
-
Save jkassemi/8265426 to your computer and use it in GitHub Desktop.
Run until all or condition met
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
drain := make(chan bool, len(tasks)) | |
answer := make(chan interface{}, 1) | |
drained := make(chan bool, 1) | |
for _, v := range tasks { | |
go func(){ | |
defer func(){ | |
drain <- true | |
}() | |
// Find an answer | |
// answer <- myAnswer | |
}() | |
} | |
go func(){ | |
for i:=0;i<len(tasks);i+=1 { | |
<-drain | |
} | |
}() | |
select { | |
case <-drained: | |
return nil | |
case a := <-answer: | |
return a | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment