Skip to content

Instantly share code, notes, and snippets.

@WhisperingChaos
Last active February 15, 2018 12:12
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 WhisperingChaos/0969b37a2586d14713a2afb42bbc50bf to your computer and use it in GitHub Desktop.
Save WhisperingChaos/0969b37a2586d14713a2afb42bbc50bf to your computer and use it in GitHub Desktop.
Nil Channel favor/bias channels:
func consumeAccordingToChannelSemantics(msg_1ch chan msg_1, msg_2ch chan msg_2) {
// because the channels were asychronous and completely full
// before running this routine, select's channel semantics
// considers the messages as having arrived at the same time.
// select therefore randomly reads one of the channels. since
// only two case statements, probability of selection is
// equivalent to a random coin toss.
for msgCnt := 0; msgCnt < 21; msgCnt++ {
select {
case msg, ok := <-msg_1ch:
if ok {
msg.msgType()
}
case msg, ok := <-msg_2ch:
if ok {
msg.msgType()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment