7

Just experimenting with go recently. I was wondering what will happen if you have a select statement waiting for communication on a few channels and if a message comes AT THE SAME time on two or more channels. How will the select determine which channel to accept if all the messages come at the same time?

Thanks!

Feras
  • 2,114
  • 3
  • 20
  • 42
  • 3
    The Golang spec is really worth a good read. At least skim through it once and then read more in depth in areas you're not familiar. You'll be glad you did :) – Brenden Nov 08 '13 at 21:45

1 Answers1

19

From the spec:

If multiple cases can proceed, a uniform pseudo-random choice is made to decide which single communication will execute.

So the choice is non-deterministic.

nemo
  • 55,207
  • 13
  • 135
  • 135
  • 4
    This non-determinism is an important part of CSP, on which Go bases its channels. Concurrent activities are strictly deterministic in CSP, which is counter-intuitive. Only when a choice is made (ie. select) does non-determinism appear. – Rick-777 Nov 10 '13 at 20:42
  • @Rick-777 [Nondeterministic Choice](https://en.wikipedia.org/w/index.php?title=Communicating_sequential_processes&oldid=943210837#Algebraic_operators) was [absent from Hoare’s original 1978 paper](https://en.wikipedia.org/w/index.php?title=Communicating_sequential_processes&oldid=943210837#History). – Shelby Moore III May 22 '20 at 14:09
  • 1
    CSP developed in stages in the late '70s and early '80s. [Occam](https://en.wikipedia.org/wiki/Occam_(programming_language)) was directly based on CSP but included only non-deterministic choice; deterministic choice was not needed. Go is like Occam in this respect. – Rick-777 May 26 '20 at 07:15