Still "missing" priority or ordered select in go?

790 views
Skip to first unread message

Øyvind Teig

unread,
Apr 29, 2021, 4:51:43 AMApr 29
to golang-nuts
I know from some years ago that go did not have any priority or ordered select construct [1].

The idea is that select always is a nondeterministic choice operator, if this is still so. Implemented with a random, I assume.

Programming user defined "fair" algorithms or patterns is then not possible, I believe.

Is this still so in Go? No prioritised or ordered select in go?

But is there still a way to code this by f.ex. combining several selects? I saw a different take at this at [2], where the select is replaced a single chan containing a chan bundle, thus becoming "deterministic".

[1] Blog note Nondeterminism (disclaimer: no ads, no gifts, only fun and expenses)

Jan Mercl

unread,
Apr 29, 2021, 5:15:14 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 10:52 AM Øyvind Teig <oyvin...@teigfam.net> wrote:

> I know from some years ago that go did not have any priority or ordered select construct [1].

Not sure what does "ordered" wrt select mean, but the default clause
of the select statement provides exactly that - a priority mechanism:
first try something, otherwise do something else. The most elementary
one, sure, but all the more complex ones can be built with this basic
building block.

Øyvind Teig

unread,
Apr 29, 2021, 5:23:54 AMApr 29
to golang-nuts
Thanks! A construct like "pri select" or "alt select" or "ordered select", so it's not a "wrt", it's a tagging of select changing ets sematics from being nondeterminstic to deterministic. 

CSP has both: external choice (nondeterminsitic), internal choice (deterministic).

The occam programming language has both "ALT" (=select) and "PRI ALT".

The xC programming language has both "select" and "[[ordered]] select".

This is not solved with a default clause, which transforms the selective choice waiting for some event to happen into busy polling. It's nice yo have some times, but that is something orthogonal to pri/ordered.

Jan Mercl

unread,
Apr 29, 2021, 5:36:45 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 11:24 AM Øyvind Teig <oyvin...@teigfam.net> wrote:

> This is not solved with a default clause, which transforms the selective choice waiting for some event to happen into busy polling. It's nice yo have some times, but that is something orthogonal to pri/ordered.

Not sure if I would call it busy polling, but I meant schematically this:

select {
case x := <-highPriority:
handle(x)
default:
select {
case x := <-highPriority:
handle(x)
case x := <-lowPriority:
handle(x)
}
}

Øyvind Teig

unread,
Apr 29, 2021, 8:26:15 AMApr 29
to golang-nuts
Interesting! 

Your suggestion would in fact do pri select in the special case 1. below:
  1. Poll highPri first (take it if it's ready), if highPri not ready then take lowPri (provided highPri has not become ready since the first poll)
  2. However, if highPri has become ready between the first and the second, then it would be taken (provided lowPri is not also ready)
  3. If both have become ready when the second select is entered they would be taken 50% of the time on the average
I fail to see that this is the general pri select that I am quering about whether it has "appeared" in go over the last years. I have a stomach feeling that it can not be implemented by polling. In the semantics of a select the whole select is evaluated before it is entered to se if there is/are any guard(s) ready. If not, pick randomly. If not, set alle guards up in some wait state.

The default case I have always used like "since no event ready (polling) then do something else than listening again on the same events". occam has deafult (although it's called TRUE & SKIP), xC does not.

Øyvind Teig

unread,
Apr 29, 2021, 8:27:47 AMApr 29
to golang-nuts
If not, pick randomly -> If so, pick randomly

Axel Wagner

unread,
Apr 29, 2021, 8:35:31 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 2:26 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
Interesting! 

Your suggestion would in fact do pri select in the special case 1. below:
  1. Poll highPri first (take it if it's ready), if highPri not ready then take lowPri (provided highPri has not become ready since the first poll)
  2. However, if highPri has become ready between the first and the second, then it would be taken (provided lowPri is not also ready)
  3. If both have become ready when the second select is entered they would be taken 50% of the time on the average

One thing to point out is that since we are talking about concurrency, where you can really only talk about something happening "after" something else happened, if there is a causal edge between them, this really *is* equivalent to a simple priority select. You can't observe the difference, unless you add more synchronization - in which case we aren't really talking about the code under discussion anymore.

I fail to see that this is the general pri select that I am quering about whether it has "appeared" in go over the last years.

I tend to agree with you on this though. So to answer that question plainly: No, there is no prioritized select in Go and I find it personally unlikely that we'll get one. It might be possible to address some use cases when we get generics, though.
 
I have a stomach feeling that it can not be implemented by polling. In the semantics of a select the whole select is evaluated before it is entered to se if there is/are any guard(s) ready. If not, pick randomly. If not, set alle guards up in some wait state.

The default case I have always used like "since no event ready (polling) then do something else than listening again on the same events". occam has deafult (although it's called TRUE & SKIP), xC does not.
torsdag 29. april 2021 kl. 11:36:45 UTC+2 skrev Jan Mercl:
On Thu, Apr 29, 2021 at 11:24 AM Øyvind Teig <oyvin...@teigfam.net> wrote:

> This is not solved with a default clause, which transforms the selective choice waiting for some event to happen into busy polling. It's nice yo have some times, but that is something orthogonal to pri/ordered.

Not sure if I would call it busy polling, but I meant schematically this:

select {
case x := <-highPriority:
handle(x)
default:
select {
case x := <-highPriority:
handle(x)
case x := <-lowPriority:
handle(x)
}
}

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/47051a51-f040-4b51-a792-24a0f96c50f4n%40googlegroups.com.

Jan Mercl

unread,
Apr 29, 2021, 8:58:23 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 2:26 PM Øyvind Teig <oyvin...@teigfam.net> wrote:

> Your suggestion would in fact do pri select in the special case 1. below:
>
> Poll highPri first (take it if it's ready), if highPri not ready then take lowPri (provided highPri has not become ready since the first poll)
> However, if highPri has become ready between the first and the second, then it would be taken (provided lowPri is not also ready)
> If both have become ready when the second select is entered they would be taken 50% of the time on the average

My analysis is different. There are four cases on entry of the outer
select statement:

1. high is ready and low is ready -> high handled, correct.
2. high is ready and low is not ready -> high handled, correct.
3. high is not ready and low is ready -> low handled, correct.

The "interesting" case is

4. high not ready and low not ready.

We enter the default clause of the outer select statement and enter
the inner select statement. Now there are three subcases when
eventually something gets ready:

4a. high gets ready before low gets ready -> high handled, correct.
4b. low gets ready before high gets ready -> low handled, correct.
4c. both high and low get ready at the same time -> one of them is
randomly chosen, correct.

Øyvind Teig

unread,
Apr 29, 2021, 9:10:31 AMApr 29
to golang-nuts
I agree with you.

However, with the first select in the example we have a "first" and with the second we have an "after" which might be considered race condition(s9 for the purpose of seeing the example of what I was after.

Also, scheduling arguments cannot be used here: like "we know that there is no scheduling (of go goroutines) between the first and the second select." If we know, it's not an argument.

Robert Engels

unread,
Apr 29, 2021, 9:20:40 AMApr 29
to Øyvind Teig, golang-nuts
It is trivial to do a priority select. Check the high priority channel again after getting a low priority value. If the high priority is available enqueue the low value and handle the high. 

It is also trivial to “delay” the handling to allow more time for high events to arrive if dealing with synchronized events and you need to account for jitter. 

On Apr 29, 2021, at 8:10 AM, Øyvind Teig <oyvin...@teigfam.net> wrote:

I agree with you.

Øyvind Teig

unread,
Apr 29, 2021, 9:23:36 AMApr 29
to golang-nuts
Jan, I agree on your list, it's better set up than mine. 

But you can add a point after (4.) though, call it (second.pre), dealing with what might have happened after the first select decided that none were taken until it evaluates the precondition to the second select. 

4c is not "correct" as I want it. In the pri select case, if more than one is ready, then they shall not be randomly chosen. Never. They should be selected according to priority.

As I discussed (and several with me) in the blog note, the fact that Go doesn't seem to have it, then go needs another definition of "fair" than what is normal discussing these things. The basic idea for Google was, if we have a problem with fairness (none gets starved, all get served), just add another computer (said over a beer). Plus, the need for the fairness that one can build with a pri select (most often by reordering the list of guards according to one's pleasing of what one might think of as fair), is according to Goodle (on go, I think), so tiny that we make it simple: drop it. Of course it's also easy to try to implement one's own fairness and fail with it. In a realtime system it's not that easy to test, and these things most often are best evaluated on paper. So a formal proof might do.

Axel Wagner

unread,
Apr 29, 2021, 9:28:03 AMApr 29
to Øyvind Teig, golang-nuts
My argument is that you can't distinguish the case "neither was ready and highPriority and lowPriority became ready simultaneously and lowPriority was selected" from "highPriority became ready after lowPriority" (as long as there is the same code in both highPriority cases). Because there is no "simultaneous" or "after" in concurrency, unless you use synchronization - the real phrasing is "highPriority and lowPriority became ready simultaneously" and if, *in real time* that means one or the other came first is indeterminate. There is no implementation or scheduling argument here - this argument is purely based on the Go memory model and the spec.

If there is no observable difference in behavior, the two are equivalent.

But either way, as I said, it's not really the same, just based on readability and clarity. It's semantically equivalent, but semantically, Go is equivalent to Assembly and yet we prefer Go :)

Øyvind Teig

unread,
Apr 29, 2021, 9:29:51 AMApr 29
to golang-nuts
I sent that to the wrong entry! Is it possible that you could make a code example?

Øyvind Teig

unread,
Apr 29, 2021, 9:38:47 AMApr 29
to golang-nuts
axel, it's difficult to try to explain in words how a select behaves. On that we can agree. For me, select cases can be channels, timeouts, external events etc. If a task is in a select (none triggered yet) and there is an interrupt that detects the select entry, it's ticked as triggered, the select is torn down, and in time the select will be taken with that event only. Not like Linux select which can deliver several causes. But if the timrout came just before the pin, then.. etc. Temporal logic in this case certainly makes sense.

Jan Mercl

unread,
Apr 29, 2021, 9:47:42 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 3:23 PM Øyvind Teig <oyvin...@teigfam.net> wrote:

> 4c is not "correct" as I want it. In the pri select case, if more than one is ready, then they shall not be randomly chosen. Never. They should be selected according to priority.

That's not what 4c says. Instead of "more than one ready" it says
"both high and low _get ready at the same time_".

Note that in the first approximation the probability of 4c happening
is approaching zero. If we consider time "ticks" in discrete quanta,
the probability is proportional to the size of the quantum. And
depending on a particular implementation of the scheduler the
probability of 4c can still be exactly zero. For example, the OS
kernel may deliver only one signal at a time to the process etc.

So the "Never" case may quite well never happen at all.

Øyvind Teig

unread,
Apr 29, 2021, 9:53:49 AMApr 29
to golang-nuts

They could still both have become ready (not in the same "cycle") between the two selects. Even if that probability is low, it would need knowledge like yours to show that this may in fact be zero. There could be a descheduling in between, one of those in my opinion, not relevant arguments.

robert engels

unread,
Apr 29, 2021, 10:10:00 AMApr 29
to Øyvind Teig, golang-nuts
I will give you the pseudo code:

{ // step #1 do select on both
   select high
   select low
}

if high read:
   return high
else:
   // we read a low so do a high poll
   {
       select high:
       default:
   }

 if high read:
      enqueue low and return high
 else:
      if queue empty:
         return low
      else:
           use queue and enqueue low from step #1 // FIFO order on low reads 

The above code will always return high values over low values (if high available), and return low values in order of events


Jan Mercl

unread,
Apr 29, 2021, 10:18:20 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 3:54 PM Øyvind Teig <oyvin...@teigfam.net> wrote:

> Even if that probability is low, it would need knowledge like yours to show that this may in fact be zero. There could be a descheduling in between, one of those in my opinion, not relevant arguments.

The upper limit of 4c happening is 1/12 for random events. I guess in
practice it is at least an order of magnitude lower because the
"effective" time ticks are small.

Ian Davis

unread,
Apr 29, 2021, 10:27:17 AMApr 29
to golan...@googlegroups.com
On Thu, 29 Apr 2021, at 3:09 PM, robert engels wrote:
I will give you the pseudo code:

{ // step #1 do select on both
   select high
   select low
}

if high read:
   return high
else:
   // we read a low so do a high poll
   {
       select high:
       default:
   }

 if high read:
      enqueue low and return high
 else:
      if queue empty:
         return low
      else:
           use queue and enqueue low from step #1 // FIFO order on low reads 

The above code will always return high values over low values (if high available), and return low values in order of events

This seems likely deadlock if the low channel is being filled continually by another goroutine. In addition I usually aim to give ownership of the channel to the writer so it can close cleanly, but mixing reads and writes in a single function greatly complicates that.

Ian

robert engels

unread,
Apr 29, 2021, 10:29:55 AMApr 29
to Øyvind Teig, golang-nuts
It needs one other fix, and that is to have a check of the queue and poll high, in case the queue has elements when the method is entered.

But if you implement the queue using a buffered channel it is trivial to change the step #1 to handle this (the low and queue channels are mimic the high and low channel).

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

robert engels

unread,
Apr 29, 2021, 10:32:11 AMApr 29
to Ian Davis, golan...@googlegroups.com
It is not.

Because the low items will be read if there are no highs available. If the low isn’t read, the producer on the low will block.

Or you run out of memory if you cannot process the events fast enough - which is also easily handled by bounding the queue - but then you may need to drop items.

There are no free lunches.

But no deadlock.



-- 
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

robert engels

unread,
Apr 29, 2021, 10:34:50 AMApr 29
to Ian Davis, golan...@googlegroups.com
To clarify, you might never process a low item if a high is always available - but I believe that is what the OP is requesting.

It is also fairly trivial to add fairness to this ti always process a low if available every N cycles.

Axel Wagner

unread,
Apr 29, 2021, 10:40:19 AMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 3:54 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
They could still both have become ready (not in the same "cycle") between the two selects. Even if that probability is low, it would need knowledge like yours to show that this may in fact be zero. There could be a descheduling in between, one of those in my opinion, not relevant arguments.

FTR, again: Yes, it's definitely possible, but it's irrelevant. It makes no observable difference. Even if we had a prioritized select, it would still be *de facto* implemented as a multi-step process and even then, you might run into exactly the same situation - you could have both channels becoming ready while the runtime does setup, or you could have a random scheduling event delaying one of the goroutines infinitesimally, or you could have…

This is why we *don't* talk about the behavior of concurrent programs in terms of cycles and time, but instead based on causal order. We don't know how long it takes to park or unpark a goroutine, so all we can say is that a read from a channel happens after the corresponding write. In terms of time, between entering the `select` statement and between parking the goroutine might lie a nanosecond, or a million years - we don't know, so we don't talk about it.

The memory model is exactly there to abstract away these differences and to not get caught up in scheduling and cycle discussions - so, FWIW, if these arguments are not relevant, you shouldn't bring them up. Logically, between the first `select` statement and the second `select` statement, there is zero time happening. Arguing that there is, is using exactly those irrelevant arguments about schedulers and processing time.
 
torsdag 29. april 2021 kl. 15:47:42 UTC+2 skrev Jan Mercl:
On Thu, Apr 29, 2021 at 3:23 PM Øyvind Teig <oyvin...@teigfam.net> wrote:

> 4c is not "correct" as I want it. In the pri select case, if more than one is ready, then they shall not be randomly chosen. Never. They should be selected according to priority.

That's not what 4c says. Instead of "more than one ready" it says
"both high and low _get ready at the same time_".

Note that in the first approximation the probability of 4c happening
is approaching zero. If we consider time "ticks" in discrete quanta,
the probability is proportional to the size of the quantum. And
depending on a particular implementation of the scheduler the
probability of 4c can still be exactly zero. For example, the OS
kernel may deliver only one signal at a time to the process etc.

So the "Never" case may quite well never happen at all.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Axel Wagner

unread,
Apr 29, 2021, 10:44:40 AMApr 29
to Øyvind Teig, golang-nuts
FWIW, maybe this helps:

Assume a read happened from lowPriority, even though highPriority was ready to read as well. That's, AIUI, the outcome you are concerned about.

In that situation, how would you know that highPriority was ready to read as well?

roger peppe

unread,
Apr 29, 2021, 2:22:32 PMApr 29
to Axel Wagner, Øyvind Teig, golang-nuts
I agree with Axel's take here. It seems, Øyvind, that you are concerned more with principle than practice here. Can you give an example of a real world case where you think that this might actually matter?

Øyvind Teig

unread,
Apr 29, 2021, 3:05:04 PMApr 29
to golang-nuts
torsdag 29. april 2021 kl. 20:22:32 UTC+2 skrev rog:
I agree with Axel's take here. It seems, Øyvind, that you are concerned more with principle than practice here. Can you give an example of a real world case where you think that this might actually matter?

Thanks, yes. I have written some about that in the Nondeterminsim blog note, referred to at the top. I admit I indicated that seeing some code might be interesting, but it was the principle I was after. In the end a "yes" or "no".

Some from the chapter "-Nondeterministic selective choice in implementations is not good": (Preceeding the quote I have been telling about CSP's external nondeterministic choice in the specfications ("implement this any way you want") but in the implementation part we have to take decisions (deterministic, inner choice: "we do it this way"). I was thinking this is relevant because Why build concurrency on the ideas of CSP? Here's the quote:

"The statement was that with the non-deterministic guarded choice in Go, what happens is up to the run-time, which is “not good”. This is implementation, not specification. With occam there is ALT or PRI ALT, always coded as PRI ALT. For a server to be “fair” I have to code it myself, it’s up to me, at the application level to find the best algorithm. Which, during my years as occam programmer was “new starting channel index in the ALT-set is the channel index of the served channel + 1 modulo-divided by number of channels”. Channels are clients[0..4] (five) ALT‘ed in set [4,0,1,2,3] served index 4, then 4+1 rem 5 == 0 yields next ALT set [0,1,2,3,4]. Just served 4 and you’re at the back of the set."

The example here is a server with N clients where it is essential that none of clients will starve and none jam the server. I have needed to do this coding several times. Go has random select which in theory may mean starving and jamming. I worked with safety critical fire detection, and it was necessary to ensure this. Or at least we didn't dare to take the chance. We could not just add another machine.

To use select when that's fair enough (pun 1) - "fair enough" (pun 2). But If I want to be certain of no starving or jamming I need to code the fairness algorithm. I can then promise a client that may have been ready but wasn't served to come in before I take the previous clients that were allowed. This is at best very difficult if all we have is select. Having pri select as the starting point is, in this case, easier.

Øyvind

Robert Engels

unread,
Apr 29, 2021, 4:02:23 PMApr 29
to Øyvind Teig, golang-nuts
I already gave you the solution to this. Trust me it works. I did HFT for 7+ years - the algo is well known. 

On Apr 29, 2021, at 2:05 PM, Øyvind Teig <oyvin...@teigfam.net> wrote:



Øyvind Teig

unread,
Apr 29, 2021, 4:46:34 PMApr 29
to golang-nuts
  1. I'm sorry I didn't follow up on your answer where you had got to the length of spelling out some code right before my eyes. I got lost in the other points (I actually started a response..)
  2. I trust you
  3. But to do more than trusting, understanding would be much better. I'm sorry: to understand I would need more than pseudo code
  4. But I was really after whether the "pri" keyword (or whatever) in front of "select" had been introduced over the last years. The answer seems to be "no"
  5. Although I appreciate that Turing proved that any language can code anything, I some times have had a hard time believing it. Maybe that's not exactly what he meant. But then, since I trust you, I do find that solution as surprising as as I would be happy to run it on The Go Playground

Axel Wagner

unread,
Apr 29, 2021, 5:02:10 PMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 10:47 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
  1. I'm sorry I didn't follow up on your answer where you had got to the length of spelling out some code right before my eyes. I got lost in the other points (I actually started a response..)
  2. I trust you
  3. But to do more than trusting, understanding would be much better. I'm sorry: to understand I would need more than pseudo code
  4. But I was really after whether the "pri" keyword (or whatever) in front of "select" had been introduced over the last years. The answer seems to be "no"

FWIW I did answer this explicitly above :) No, there is no way to manually specify priorities of `select` and no, I subjectively wouldn't predict it happening at any point in the near or far future. Unless someone comes up with an extremely convincing, so far unheard of argument in its favor :)
 

roger peppe

unread,
Apr 29, 2021, 5:52:05 PMApr 29
to Øyvind Teig, golang-nuts
On Thu, 29 Apr 2021, 20:05 Øyvind Teig, <oyvin...@teigfam.net> wrote:
torsdag 29. april 2021 kl. 20:22:32 UTC+2 skrev rog:
I agree with Axel's take here. It seems, Øyvind, that you are concerned more with principle than practice here. Can you give an example of a real world case where you think that this might actually matter?

Thanks, yes. I have written some about that in the Nondeterminsim blog note, referred to at the top. I admit I indicated that seeing some code might be interesting, but it was the principle I was after. In the end a "yes" or "no".

Some from the chapter "-Nondeterministic selective choice in implementations is not good": (Preceeding the quote I have been telling about CSP's external nondeterministic choice in the specfications ("implement this any way you want") but in the implementation part we have to take decisions (deterministic, inner choice: "we do it this way"). I was thinking this is relevant because Why build concurrency on the ideas of CSP? Here's the quote:

"The statement was that with the non-deterministic guarded choice in Go, what happens is up to the run-time, which is “not good”. This is implementation, not specification. With occam there is ALT or PRI ALT, always coded as PRI ALT. For a server to be “fair” I have to code it myself, it’s up to me, at the application level to find the best algorithm. Which, during my years as occam programmer was “new starting channel index in the ALT-set is the channel index of the served channel + 1 modulo-divided by number of channels”. Channels are clients[0..4] (five) ALT‘ed in set [4,0,1,2,3] served index 4, then 4+1 rem 5 == 0 yields next ALT set [0,1,2,3,4]. Just served 4 and you’re at the back of the set."

The example here is a server with N clients where it is essential that none of clients will starve and none jam the server.
I have needed to do this coding several times. Go has random select which in theory may mean starving and jamming. I worked with safety critical fire detection, and it was necessary to ensure this. Or at least we didn't dare to take the chance. We could not just add another machine.

To use select when that's fair enough (pun 1) - "fair enough" (pun 2). But If I want to be certain of no starving or jamming I need to code the fairness algorithm. I can then promise a client that may have been ready but wasn't served to come in before I take the previous clients that were allowed. This is at best very difficult if all we have is select. Having pri select as the starting point is, in this case, easier.

To start with, if you've got N clients where N isn't known in advance, it's not possible to use Go's select statement directly because it doesn't provide support for reading from a slice.
You can do it with reflection though. It's not too hard to code something quite similar to your algorithm above.

I think the above code should fit your definition of fairness, and it seems to me that it's a reasonably general approach.
 
  cheers,
    rog.

Ian Lance Taylor

unread,
Apr 29, 2021, 8:45:02 PMApr 29
to Øyvind Teig, golang-nuts
On Thu, Apr 29, 2021 at 12:05 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
>
> The example here is a server with N clients where it is essential that none of clients will starve and none jam the server. I have needed to do this coding several times. Go has random select which in theory may mean starving and jamming. I worked with safety critical fire detection, and it was necessary to ensure this. Or at least we didn't dare to take the chance. We could not just add another machine.
>
> To use select when that's fair enough (pun 1) - "fair enough" (pun 2). But If I want to be certain of no starving or jamming I need to code the fairness algorithm. I can then promise a client that may have been ready but wasn't served to come in before I take the previous clients that were allowed. This is at best very difficult if all we have is select. Having pri select as the starting point is, in this case, easier.

When there are multiple ready results, the select statement in Go is
guaranteed to use a uniform pseudo-random selection
(https://golang.org/ref/spec#Select_statements). The fact that cases
are selected using a uniform distribution ensures that executing a
select statement in a loop can't lead to starving (I'm not sure what
jamming is).

As several people have said, it's meaningless to say "I want to ensure
that if both case A and case B are available then the select statement
will choose case A." It's meaningless because it relies on a notion
of simultaneity that literally doesn't exist. There will always be a
period of time after the select statement chooses B, but before it
actually executes the case, that case A may become ready. That will
be true for any possible implementation of select, because select is
not an atomic operation.

Therefore, this code using a hypothetical priority case

select {
case pri <-c1:
case <-c2:
}

can always be rewritten as

select {
case <-c1:
default:
select {
case <-c1:
case <-c2:
}
}

The rewritten select is exactly equivalent to the first. The only way
that they could not be equivalent would be if select were atomic.

Ian

Øyvind Teig

unread,
Apr 30, 2021, 3:20:20 AMApr 30
to golang-nuts
torsdag 29. april 2021 kl. 23:52:05 UTC+2 skrev rog:
On Thu, 29 Apr 2021, 20:05 Øyvind Teig, <oyvin...@teigfam.net> wrote:
torsdag 29. april 2021 kl. 20:22:32 UTC+2 skrev rog:
I agree with Axel's take here. It seems, Øyvind, that you are concerned more with principle than practice here. Can you give an example of a real world case where you think that this might actually matter?

Thanks, yes. I have written some about that in the Nondeterminsim blog note, referred to at the top. I admit I indicated that seeing some code might be interesting, but it was the principle I was after. In the end a "yes" or "no".

Some from the chapter "-Nondeterministic selective choice in implementations is not good": (Preceeding the quote I have been telling about CSP's external nondeterministic choice in the specfications ("implement this any way you want") but in the implementation part we have to take decisions (deterministic, inner choice: "we do it this way"). I was thinking this is relevant because Why build concurrency on the ideas of CSP? Here's the quote:

"The statement was that with the non-deterministic guarded choice in Go, what happens is up to the run-time, which is “not good”. This is implementation, not specification. With occam there is ALT or PRI ALT, always coded as PRI ALT. For a server to be “fair” I have to code it myself, it’s up to me, at the application level to find the best algorithm. Which, during my years as occam programmer was “new starting channel index in the ALT-set is the channel index of the served channel + 1 modulo-divided by number of channels”. Channels are clients[0..4] (five) ALT‘ed in set [4,0,1,2,3] served index 4, then 4+1 rem 5 == 0 yields next ALT set [0,1,2,3,4]. Just served 4 and you’re at the back of the set."

The example here is a server with N clients where it is essential that none of clients will starve and none jam the server.
I have needed to do this coding several times. Go has random select which in theory may mean starving and jamming. I worked with safety critical fire detection, and it was necessary to ensure this. Or at least we didn't dare to take the chance. We could not just add another machine.

To use select when that's fair enough (pun 1) - "fair enough" (pun 2). But If I want to be certain of no starving or jamming I need to code the fairness algorithm. I can then promise a client that may have been ready but wasn't served to come in before I take the previous clients that were allowed. This is at best very difficult if all we have is select. Having pri select as the starting point is, in this case, easier.

To start with, if you've got N clients where N isn't known in advance, it's not possible to use Go's select statement directly because it doesn't provide support for reading from a slice.
You can do it with reflection though. It's not too hard to code something quite similar to your algorithm above.

Thanks! I like the fact that you can do it like this. However, there was "client 2" in the log(?)

Øyvind

Øyvind Teig

unread,
Apr 30, 2021, 3:53:21 AMApr 30
to golang-nuts
fredag 30. april 2021 kl. 02:45:02 UTC+2 skrev Ian Lance Taylor:
On Thu, Apr 29, 2021 at 12:05 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
>
> The example here is a server with N clients where it is essential that none of clients will starve and none jam the server. I have needed to do this coding several times. Go has random select which in theory may mean starving and jamming. I worked with safety critical fire detection, and it was necessary to ensure this. Or at least we didn't dare to take the chance. We could not just add another machine.
>
> To use select when that's fair enough (pun 1) - "fair enough" (pun 2). But If I want to be certain of no starving or jamming I need to code the fairness algorithm. I can then promise a client that may have been ready but wasn't served to come in before I take the previous clients that were allowed. This is at best very difficult if all we have is select. Having pri select as the starting point is, in this case, easier.

When there are multiple ready results, the select statement in Go is
guaranteed to use a uniform pseudo-random selection
(https://golang.org/ref/spec#Select_statements). The fact that cases
are selected using a uniform distribution ensures that executing a
select statement in a loop can't lead to starving (I'm not sure what
jamming is).

As several people have said, it's meaningless to say "I want to ensure
that if both case A and case B are available then the select statement
will choose case A." It's meaningless because it relies on a notion
of simultaneity that literally doesn't exist.

I have a stomach feeling that I agree with you. I will still argue against. I just wish someone could pinpoint where these standpoints clash, or don't. 

If there is no notion of simultaneity why all the effort to describe the random distribution? 

The select is first set up, at which time the code decides on which one to take if more than one guard is ready. If the clients were only sending, then nowhere in the system is this noted on "the other" side of the channel (in the server) before it enters the select. The channel would have noted the first contender, yes, but the servre have yet no idea. If none is ready, then the server was first on all the ends, and when a sender arrives it will match the guard set in the server and tear down the select. In due time the server is scheduled with that one event.

This is how I have seen it in several systems. I wonder what might be so different with go.
 
There will always be a
period of time after the select statement chooses B, but before it
actually executes the case, that case A may become ready. That will
be true for any possible implementation of select, because select is
not an atomic operation.

Therefore, this code using a hypothetical priority case

select {
case pri <-c1:
case <-c2:
}

can always be rewritten as

select {
case <-c1:
default:
select {
case <-c1:
case <-c2:
}
}

The rewritten select is exactly equivalent to the first. The only way
that they could not be equivalent would be if select were atomic.

Ok, so this is a pattern that Go people would use if they needed to do pri select. Then, why go to the lengths of the other code shown above? Is it because I have kind of "pressed" you to come up with code and then of course, one thing may be solved several ways? 

Will your Go code examples stand the test of formal verification? Of course, when it's not formally verified you probaby could not answer such a question. But the stomach feeling?

Another angle: Go does not have the expression before the select that evaluates to true or false. Nothing like

select { 
case (do_this) => val1 <-c1: 
case val2  <-c2: 

Instead, the chan is set to nil to exclude it from the set. What might happen if we had a set of 100 clients and they were switched on and off internally in the server (that's their purpose) - when will the uniform distribution be reset? What's the life span of the distribution? With a psudorandom sequence any one value is only visited once on a round. We still want this to be fair. Could those having been served be served again (before the others) after a reset of the distribution, and this introduce a notion of unfairness?

 (I gues that jamming is that only one client alone gets to the server, whereas starving is that a client never gets to the server).

Øyvind
 

Ian

Axel Wagner

unread,
Apr 30, 2021, 4:42:47 AMApr 30
to golang-nuts
On Fri, Apr 30, 2021 at 9:53 AM Øyvind Teig <oyvin...@teigfam.net> wrote:
If there is no notion of simultaneity why all the effort to describe the random distribution?

While it's not possible for two cases to become ready at the same time, it's definitely possible for two cases to be ready when entering a select. That's where the random selection comes in.

There's also the notable difference between a select with a default and one without. A select with a default never blocks, so which branch is taken is *only* determined by what's ready when entering the select, whereas a select without can block and then gets woken up by the first communication that's ready - and there'll always be a "first".

In a sense, the nested select uses that: The outer select handles the "what's currently ready" case and the inner select handles the "what becomes ready in the future".

The priority select would use the same basic logic:
- Is the high priority case ready? If so, do that
- If not, block until one of the cases become ready - do the first that becomes ready

The crux here is exactly that we can't have two cases "becoming ready" at the same time, so we really *have* to "take the first one that becomes ready".

The select is first set up, at which time the code decides on which one to take if more than one guard is ready. If the clients were only sending, then nowhere in the system is this noted on "the other" side of the channel (in the server) before it enters the select. The channel would have noted the first contender, yes, but the servre have yet no idea. If none is ready, then the server was first on all the ends, and when a sender arrives it will match the guard set in the server and tear down the select. In due time the server is scheduled with that one event.

This is how I have seen it in several systems. I wonder what might be so different with go.

I don't think I understand this exposition. But on first glance, your description doesn't sound terribly different from what's happening in Go.

To be clear: No one is claiming it would be impossible to implement a priority select in Go. Obviously we could replace the pseudo-random choice by something else. We are just saying that it would be equivalent to the nested select code.

Ok, so this is a pattern that Go people would use if they needed to do pri select. Then, why go to the lengths of the other code shown above? Is it because I have kind of "pressed" you to come up with code and then of course, one thing may be solved several ways? 

I think the first code you where shown by Jan (which is the same as Ian's) is correct and I believe it's likely that your insistence that it isn't is what prompted people to come up with more and more complicated code.

Will your Go code examples stand the test of formal verification? Of course, when it's not formally verified you probaby could not answer such a question. But the stomach feeling?

I'm not very familiar with formal methods for this, or what the invariant is that would be verified.
I do feel quite confident about the statement that the shown snippet is equivalent to how I'd think a priority select would work.

Another angle: Go does not have the expression before the select that evaluates to true or false. Nothing like

select { 
case (do_this) => val1 <-c1: 
case val2  <-c2: 

Instead, the chan is set to nil to exclude it from the set. What might happen if we had a set of 100 clients and they were switched on and off internally in the server (that's their purpose) - when will the uniform distribution be reset? What's the life span of the distribution? With a psudorandom sequence any one value is only visited once on a round.

I'm not sure what you mean here. Is what you call a "round" the cycle of the PRNG? In that case, this statement isn't true, the cycle is likely significantly longer than the number of cases. So we definitely chose at least one case multiple times per cycle.

AFAIK this is the PRNG used by the select, FWIW. I assume it simply calls into it (or likely `fastrandn` directly below) when entering a select with multiple available cases.

We still want this to be fair. Could those having been served be served again (before the others) after a reset of the distribution, and this introduce a notion of unfairness?

It can definitely happen, but I'm not sure that "unfairness" is a meaningful term here. AIUI the process is "if the runtime enters a select and multiple cases are ready, it chooses one uniformly at random" (within the limits of the PRNG). Yes, as an outcome this can mean that one case is hit more often than the others. But all cases are equally likely to be hit more often. And by the law of large numbers, you'd expect the distribution to flatten over time.

 (I gues that jamming is that only one client alone gets to the server, whereas starving is that a client never gets to the server).

Both are statistically unlikely, if we assume the PRNG is reasonably good - which I think we can, it has been subjected to reasonable statistical tests.
 

Øyvind
 

Ian

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.

Øyvind Teig

unread,
May 2, 2021, 3:23:13 PMMay 2
to golang-nuts
Axel, it is impolite not to try to comment and discuss each and every point above. I actually started to do exactly that when I discovered that it all boils down to this (FWIW?):

I have tried to expand on Jan's code (https://go2goplay.golang.org/p/7xDzP6Jvyl8), here: https://go2goplay.golang.org/p/vhmo_Vw6OQy. I have added a mediumPriority channel. (Hope it's right..)

Ian said that select is not an atomic operation. I assume (but everyone here seems to tell me the opposite), that at each default there are starts of new, unique selects? 

Here is one of the comments I wrote to one of Axel's points above, and it could be iterated over three priorities as well:

I think this is where I need to understand Go a little better, because it might be different from occam's default (TRUE & SKIP). Actually, this may be the reason why this thread is still not closed. To me it is very strange that between the first polling of the highPri and the default, why that outer select is not torn down. Then enter a new select, which would have two guards: high and low pri. In my head when setting up the new select there would be a decision of which one to select. It would select from the set of ready guards right there. They could both have become ready. Remember in my head these two may be hw pins. (If the first high pri poll was done at 0 ns and the second select's decision could be 10 ns later, then both hw pins could have become ready at 5 ns). If so the decision needs to be on one of them. With "only random" (yes, I think think this is so, on a general basis, but I accept that Go doesn't have the other option) to chose from, then it may chose the low pri, even if the high pri also was, hw wise, ready. 

If these two (or three) cannot be hardware pins (as in Go), then I reason (by induction(?)) that all of the code must be atomic with no descheduling in between, for me to understand that the scheme is 100% as intended: meaning that there is not any state where random select is ever used. 

rog wrote above (where I had indicated that occam (and also xC, said here) has a looping channel construct): "To start with, if you've got N clients where N isn't known in advance, it's not possible to use Go's select statement directly because it doesn't provide support for reading from a slice." Does this mean that aside from reflection (https://go2goplay.golang.org/p/S_5WFkpqMP_H - which still does not serve "client 2", shouldn't it?) then idiomatic Go for a small number of priorities is the one with default case(s), and it works 100% as intended, with no cognitive (?) reliance on Go's inner working under the hood? (I mean: "WYSIWYG semantics" kind of.)

I am at a point now that if the answer to the above is yes, I'll just say thank you for your help, and I will be a Go-wise wiser person. With my cognitive bias I will then have to accept that this is Go, nothing more to say. Just accept it. Anyhow, in case, thank you!

Øyvind

Øyvind Teig

unread,
May 2, 2021, 3:32:02 PMMay 2
to golang-nuts
One more thing, if (by presumably faulty reasoning above) random select is never done, would we need the two guards in the second select? 

Axel Wagner

unread,
May 2, 2021, 3:42:10 PMMay 2
to Øyvind Teig, golang-nuts
On Sun, May 2, 2021 at 9:23 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
Axel, it is impolite not to try to comment and discuss each and every point above.

I wasn't trying to be impolite. But I also won't go through your messages sentence by sentence, trying to say something even if I have nothing to say. Sorry. If that doesn't suit you, I'll step out of the conversation.
 
I have tried to expand on Jan's code (https://go2goplay.golang.org/p/7xDzP6Jvyl8), here: https://go2goplay.golang.org/p/vhmo_Vw6OQy. I have added a mediumPriority channel. (Hope it's right..)

That code is missing a case in the innermost select. This one seems correct:
 
Ian said that select is not an atomic operation. I assume (but everyone here seems to tell me the opposite), that at each default there are starts of new, unique selects? 

Here is one of the comments I wrote to one of Axel's points above, and it could be iterated over three priorities as well:

I think this is where I need to understand Go a little better, because it might be different from occam's default (TRUE & SKIP). Actually, this may be the reason why this thread is still not closed. To me it is very strange that between the first polling of the highPri and the default, why that outer select is not torn down. Then enter a new select, which would have two guards: high and low pri. In my head when setting up the new select there would be a decision of which one to select. It would select from the set of ready guards right there. They could both have become ready.

This description sounds correct. This is how Go behaves.
 
Remember in my head these two may be hw pins. (If the first high pri poll was done at 0 ns and the second select's decision could be 10 ns later, then both hw pins could have become ready at 5 ns). If so the decision needs to be on one of them. With "only random" (yes, I think think this is so, on a general basis, but I accept that Go doesn't have the other option) to chose from, then it may chose the low pri, even if the high pri also was, hw wise, ready.

This is fundamentally correct (though I'm not sure what you mean by "hw pin").
 
If these two (or three) cannot be hardware pins (as in Go), then I reason (by induction(?)) that all of the code must be atomic with no descheduling in between, for me to understand that the scheme is 100% as intended: meaning that there is not any state where random select is ever used.

It is.

Again: Your understanding is correct. But the resulting situation is still equivalent to a priority select. There is no observable behavior in difference between the two.
So let me repeat my question:

Assume a read happened from lowPriority, even though highPriority was ready to read as well. That's, AIUI, the outcome you are concerned about.
In that situation, how would you know that highPriority was ready to read as well?

I believe you'll find that the answer is "you can't".

 

Øyvind Teig

unread,
May 2, 2021, 5:55:50 PMMay 2
to golang-nuts
Axel, I was saying (at least meaning) that it was impolite of me not to answer your exhaustive comments! The other way around. But I now see why you got upset. It may read that way too! I read things over many times, did spell checking, but I didn't see this coming! I will read the rest of the comments tomorrow! Please excuse me for my Bad English! I have always been impressed by the golang-nuts group, how well everything has been responded to, and how much people have taken the time to answer my questions, coming from the other side of the field. I don't take this for granted at all! Øyvind

Axel Wagner

unread,
May 3, 2021, 3:24:45 AMMay 3
to golang-nuts
No worries, I was not upset :) But I did misunderstand you, thanks for clearing that up.

Øyvind Teig

unread,
May 3, 2021, 12:34:20 PMMay 3
to golang-nuts
søndag 2. mai 2021 kl. 21:42:10 UTC+2 skrev axel.wa...@googlemail.com:
On Sun, May 2, 2021 at 9:23 PM Øyvind Teig <oyvin...@teigfam.net> wrote:

(Removed clumsy sentence from me. Again: sorry!) Thanks for all this answering, Axel!

I have tried to expand on Jan's code (https://go2goplay.golang.org/p/7xDzP6Jvyl8), here: https://go2goplay.golang.org/p/vhmo_Vw6OQy. I have added a mediumPriority channel. (Hope it's right..)

That code is missing a case in the innermost select. This one seems correct:

Great! The rule is high in all selects, medium in two lowest, low only the last select.

Ian said that select is not an atomic operation. I assume (but everyone here seems to tell me the opposite), that at each default there are starts of new, unique selects? 

Here is one of the comments I wrote to one of Axel's points above, and it could be iterated over three priorities as well:

I think this is where I need to understand Go a little better, because it might be different from occam's default (TRUE & SKIP). Actually, this may be the reason why this thread is still not closed. To me it is very strange that between the first polling of the highPri and the default, why that outer select is not torn down. Then enter a new select, which would have two guards: high and low pri. In my head when setting up the new select there would be a decision of which one to select. It would select from the set of ready guards right there. They could both have become ready.

This description sounds correct. This is how Go behaves.

Great. I hoped so. Even if it migth get me into trouble below.
  
Remember in my head these two may be hw pins. (If the first high pri poll was done at 0 ns and the second select's decision could be 10 ns later, then both hw pins could have become ready at 5 ns). If so the decision needs to be on one of them. With "only random" (yes, I think think this is so, on a general basis, but I accept that Go doesn't have the other option) to chose from, then it may chose the low pri, even if the high pri also was, hw wise, ready.

This is fundamentally correct (though I'm not sure what you mean by "hw pin").

Great, I hoped so (2). Even if it migth get me into trouble below (2).

"hw pin": On xC (by XMOS) this is valid: https://go2goplay.golang.org/p/Min0kAI7Wao (it looked so much like Go that formatting was ok!)
Observe the clause between "case" and "=>" which is the expression, it has to be true to enable the guard. All guards false is an error.

If these two (or three) cannot be hardware pins (as in Go), then I reason (by induction(?)) that all of the code must be atomic with no descheduling in between, for me to understand that the scheme is 100% as intended: meaning that there is not any state where random select is ever used.

It is.

Trouble A: If random select is never used in the code example, then (as I queried after this text yesterday) why do we need to have more than one component in each select? Like this https://go2goplay.golang.org/p/ttr7sojJ-9T ??

Again: Your understanding is correct. But the resulting situation is still equivalent to a priority select. There is no observable behavior in difference between the two.
So let me repeat my question:

Assume a read happened from lowPriority, even though highPriority was ready to read as well. That's, AIUI, the outcome you are concerned about.
In that situation, how would you know that highPriority was ready to read as well?

I believe you'll find that the answer is "you can't".

Trouble B: Since I think you have affirmed my scenarios above, that on each select's default there is a tear-down of the previous select and a set-up of the next select, which initially checks the guards to see if there is any, and in case more than two, does random select and selects the wrong (0 ns, 5 ns, 10 ns, 15 ns..). So I would say, "yes, I can". 

Again, where do I go wrong in this argument (when I think you confirmed..)?

You are all allowed to give up on me at this point.. (But I don't seem to be able to trust your truth without this reflection.)

If so, maybe a constructive point is to try to write some runnable Go code that uses this pri select pattern. I have thought about it, but I don't know how to check whether it would ever enter one of the selects and in fact need to do a random select, and select a lower when a higher is present (or became present). Plus, if I try to synchronise clients and send over sequence counts, the scheduling pattern could become so repetitive that no such situation would occur. 

Is there a way to inspect the built code and do it from code inspection? (I guess so?)

But for all this, I would need even more help...

(Maybe I'll try to trigger a student since I have so much xC ahead of me..)

Øyvind

Axel Wagner

unread,
May 3, 2021, 2:23:09 PMMay 3
to Øyvind Teig, golang-nuts
On Mon, May 3, 2021 at 6:34 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
meaning that there is not any state where random select is ever used.
It is.
Trouble A: If random select is never used […]

I was unclear: When I said "It is", I meant "it is used". Your understanding of what select does is correct. There will be cases, where both channels are ready when the inner `select` is entered and thus, there will be a pseudorandom choice over which will proceed.

The argument put forward is that that's exactly how a priority select would have to behave as well. As least as I imagine it and as well as I understand the implementation of `select` (which is, admittedly, not *super* well).

If so, maybe a constructive point is to try to write some runnable Go code that uses this pri select pattern. I have thought about it, but I don't know how to check whether it would ever enter one of the selects

This is precisely what my question was getting at. FWIW, it's ultimately  pretty straight forward to demonstrate this behavior:
This program exits if and only if the inner select choses `lo`. Given that we know that `hi` is being closed before `lo` ("Within a single goroutine, the happens-before order is the order expressed by the program."), `lo` being ready implies `hi` being ready as well. Thus, by seeing that the program exits, we can show that the inner select sometimes chooses the `lo` case, even though the `hi` is ready as well.

Crucially, however, we needed to know that `close(hi)` happens before `close(lo)` to prove this case was taken - thus, the communications are not concurrent. That's what I meant by "external synchronization primitives".

I think my question was flawed, because really, the issue isn't about how the `select` with `default` construct we showed works - the question is how a priority `select` could work. That is, could we implement a priority `select` such that this code terminates: https://play.golang.org/p/4G8CY36L0Qy

I don't think we can - and based on that assumption I extrapolated how a priority select would actually behave - but I have to admit that I really don't understand `select` or the underlying hardware primitives enough to make a solid case either way here. Maybe you can provide an equivalent program in a language of your choice that terminates - that would certainly prove that it's at least possible (though to be clear: I don't understand your xC code, so I can't promise that I'd understand whatever you send here, personally :) ).
 
All of that being said: I really think that in the cases where a priority select is needed, this construct is good enough to hold up. 

Robert Engels

unread,
May 3, 2021, 3:11:48 PMMay 3
to Axel Wagner, Øyvind Teig, golang-nuts
In most “select” implementations a set of “ready” endpoints is returned. So it is trivial for the reader to prioritize some endpoints over others. 

Because of the way Go select works it is more difficult - requiring nested selects - and it is more easily implemented using multiple readers and queues once it moves beyond a few producers. 

On May 3, 2021, at 1:23 PM, 'Axel Wagner' via golang-nuts <golan...@googlegroups.com> wrote:



Øyvind Teig

unread,
May 3, 2021, 3:16:09 PMMay 3
to golang-nuts
mandag 3. mai 2021 kl. 20:23:09 UTC+2 skrev axel.wa...@googlemail.com:
On Mon, May 3, 2021 at 6:34 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
meaning that there is not any state where random select is ever used.
It is.
Trouble A: If random select is never used […]

I was unclear: When I said "It is", I meant "it is used". Your understanding of what select does is correct. There will be cases, where both channels are ready when the inner `select` is entered and thus, there will be a pseudorandom choice over which will proceed.

Great!

The argument put forward is that that's exactly how a priority select would have to behave as well. As least as I imagine it and as well as I understand the implementation of `select` (which is, admittedly, not *super* well).

That is where we might disagree, or at least I not understand you. 

If so, maybe a constructive point is to try to write some runnable Go code that uses this pri select pattern. I have thought about it, but I don't know how to check whether it would ever enter one of the selects

This is precisely what my question was getting at. FWIW, it's ultimately  pretty straight forward to demonstrate this behavior:

You seem to write Go code fast!-) I don't see where hi and lo are being sent to? Maybe add some more prints would make me understand it better. I would have expected something like https://en.wikipedia.org/wiki/Go_(programming_language)#Concurrencyexample 

Maybe this is not needed since you run another type of reasoning:

This program exits if and only if the inner select choses `lo`. Given that we know that `hi` is being closed before `lo` ("Within a single goroutine, the happens-before order is the order expressed by the program."), `lo` being ready implies `hi` being ready as well. Thus, by seeing that the program exits, we can show that the inner select sometimes chooses the `lo` case, even though the `hi` is ready as well. 
Crucially, however, we needed to know that `close(hi)` happens before `close(lo)` to prove this case was taken - thus, the communications are not concurrent. That's what I meant by "external synchronization primitives".

But then, "happens before" is "within a single gorotine". When it comes to several and concurrent goroutines there is no other order than those forced on them by synchronisation over nonbuffered channels (or the join you had in the code example). And then implicit ordering that comes from repetive patterns in the scheduling and running that may cause difficult situations never to happen.

I think my question was flawed, because really, the issue isn't about how the `select` with `default` construct we showed works - the question is how a priority `select` could work. That is, could we implement a priority `select` such that this code terminates: https://play.golang.org/p/4G8CY36L0Qy

 
I don't think we can - and based on that assumption I extrapolated how a priority select would actually behave - but I have to admit that I really don't understand `select` or the underlying hardware primitives enough to make a solid case either way here. Maybe you can provide an equivalent program in a language of your choice that terminates - that would certainly prove that it's at least possible (though to be clear: I don't understand your xC code, so I can't promise that I'd understand whatever you send here, personally :) ).
 
All of that being said: I really think that in the cases where a priority select is needed, this construct is good enough to hold up. 

Hmm.. I must admit I smile here. I kind of like that we come to different conclusions. Would you you send your daughter with a fly by wire airplane that has some "good enough" sw? I have worked with safety critical systems all my profession life (40+ years), and we certainly had to find the root cause if we suspected we arrived there. Of course, risk (probability * conesequence) is never zero. Plus, they don't rely on a single system.

There is a rather good explanation of PRI ALT (=pri select) of occam, where the TRUE & SKIP (=default) is also seen at page 72 of http://www.transputer.net/obooks/isbn-013629312-3/oc20refman.pdf. This is occam, where much of Limbo and later Go came from, with regards to concurrency (CSP). The Go designers made their own choices. However, in knowing more than we think we need to know it may be an ok ref. There also is a manual in how to write compilers for this, but the PRI par is not mentioned I think, simply because I think that the transputer in fact only had PRI PAR: http://www.transputer.net/iset/pdf/tis-acwg.pdf

Øyvind

Øyvind Teig

unread,
May 3, 2021, 3:24:24 PMMay 3
to golang-nuts
mandag 3. mai 2021 kl. 21:11:48 UTC+2 skrev ren...@ix.netcom.com:
In most “select” implementations a set of “ready” endpoints is returned. So it is trivial for the reader to prioritize some endpoints over others. 

Do you mean in user code in other languages than Go? If so, which ones were you thinking of? 

Because of the way Go select works it is more difficult - requiring nested selects - and it is more easily implemented using multiple readers and queues once it moves beyond a few producers. 

I see that, which is great. But I still don't understand why https://go2goplay.golang.org/p/S_5WFkpqMP_H (By rog, 29Apr2021 23:52:05) seems not to print "Client 2".

Øyvind

Axel Wagner

unread,
May 3, 2021, 3:44:49 PMMay 3
to Øyvind Teig, golang-nuts
On Mon, May 3, 2021 at 9:16 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
I don't see where hi and lo are being sent to?

They are being `close`d. Reading from a closed channel immediately succeeds, yielding the zero value.
I chose to use `close` because it's a non-blocking way to make a channel readable. You could get the same demonstration by making the channels buffered and writing to them.

But then, "happens before" is "within a single gorotine". When it comes to several and concurrent goroutines there is no other order than those forced on them by synchronisation over nonbuffered channels (or the join you had in the code example).

Exactly. It is only *because* we use a single goroutine and we thus know that there is a happens-before edge.
For example, this program *also* exits (i.e. line 16 is executed), but you can no longer conclude that `hi` was ready when it does.

This is how most practical cases would likely happen.

I think my question was flawed, because really, the issue isn't about how the `select` with `default` construct we showed works - the question is how a priority `select` could work. That is, could we implement a priority `select` such that this code terminates: https://play.golang.org/p/4G8CY36L0Qy


I don't understand this question. Though I also see that my question is wrong - it should be "could we implement a priority `select` such that this code *never* terminates.

If we suppose a perfect priority select, which never chooses the `lo` case if the `hi` case is ready, the program I linked would never terminate. Because `lo` is never ready unless `hi` is ready too. This, AIUI, is the priority `select` you'd want. Note, in particular, the made up `pri` keyword in the example.

So, no, in that scenario we wouldn't have a pseudo-random choice. The question I was begging is if we could actually implement a select like you want. If you can write this program in another language, and that program ends up not terminating, that would demonstrate that it's indeed possible to write a `select` as you are requesting.

I don't think we can - and based on that assumption I extrapolated how a priority select would actually behave - but I have to admit that I really don't understand `select` or the underlying hardware primitives enough to make a solid case either way here. Maybe you can provide an equivalent program in a language of your choice that terminates - that would certainly prove that it's at least possible (though to be clear: I don't understand your xC code, so I can't promise that I'd understand whatever you send here, personally :) ).
 
All of that being said: I really think that in the cases where a priority select is needed, this construct is good enough to hold up. 

Hmm.. I must admit I smile here. I kind of like that we come to different conclusions. Would you you send your daughter with a fly by wire airplane that has some "good enough" sw?

This is… a strange question. I'm not saying "the software might be buggy, but it will be buggy software that is good enough". I'm saying "the construct is good enough to implement non-buggy software with it". Of course you need to know what the construct does and take it into account when writing your code - if you assume it works differently than it does, you'll introduce bugs, yes. That is true in any language.
 
There is a rather good explanation of PRI ALT (=pri select) of occam, where the TRUE & SKIP (=default) is also seen at page 72 of http://www.transputer.net/obooks/isbn-013629312-3/oc20refman.pdf.

Can you translate the example into that language? And demonstrate that it doesn't terminate?
 
It's not practical for me to learn a different language and scour its reference manual, to try and figure out how their select works, if that is compatible with how Go's select works and thus if and how lessons learned from that language are transferable to Go.

You seem to be convinced that their priority select is superior to Go's select construct and can map semantics that Go can't. I'm willing to believe that, but I'd prefer a demonstration. The example should, in a loop
• create two channels `hi` and `lo`
• In a concurrent routine, write first to `hi` and then to `lo` (to establish a happens-before edge)
• use a priority-select
• prioritizing reading from `hi`
• exiting the program if it reads from `lo`
• never exit, when run
If you can make this translation, I'm convinced that it's possible implementing a `select` statement using the semantics you suggest. Without that - no offense - I must consider the possibility that you are misunderstanding how Occam's select statement works.

Robert Engels

unread,
May 3, 2021, 3:48:20 PMMay 3
to Øyvind Teig, golang-nuts
Yes, C for instance. 

It doesn’t print ‘client 2’ because your code is broken. It prints “late arrival” if you remove the sleep. The main program finishes before the go routine runs.  

On May 3, 2021, at 2:24 PM, Øyvind Teig <oyvin...@teigfam.net> wrote:

Øyvind Teig

unread,
May 3, 2021, 5:12:03 PMMay 3
to golang-nuts
mandag 3. mai 2021 kl. 21:44:49 UTC+2 skrev axel.wa...@googlemail.com:
On Mon, May 3, 2021 at 9:16 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
I don't see where hi and lo are being sent to?

They are being `close`d. Reading from a closed channel immediately succeeds, yielding the zero value.
I chose to use `close` because it's a non-blocking way to make a channel readable. You could get the same demonstration by making the channels buffered and writing to them.

But then, "happens before" is "within a single gorotine". When it comes to several and concurrent goroutines there is no other order than those forced on them by synchronisation over nonbuffered channels (or the join you had in the code example).

Exactly. It is only *because* we use a single goroutine and we thus know that there is a happens-before edge.
For example, this program *also* exits (i.e. line 16 is executed), but you can no longer conclude that `hi` was ready when it does.

This is how most practical cases would likely happen.

I think my question was flawed, because really, the issue isn't about how the `select` with `default` construct we showed works - the question is how a priority `select` could work. That is, could we implement a priority `select` such that this code terminates: https://play.golang.org/p/4G8CY36L0Qy


I don't understand this question. Though I also see that my question is wrong - it should be "could we implement a priority `select` such that this code *never* terminates.

If we suppose a perfect priority select, which never chooses the `lo` case if the `hi` case is ready, the program I linked would never terminate. Because `lo` is never ready unless `hi` is ready too. This, AIUI, is the priority `select` you'd want. Note, in particular, the made up `pri` keyword in the example.

So, no, in that scenario we wouldn't have a pseudo-random choice. The question I was begging is if we could actually implement a select like you want. If you can write this program in another language, and that program ends up not terminating, that would demonstrate that it's indeed possible to write a `select` as you are requesting.

I don't think we can - and based on that assumption I extrapolated how a priority select would actually behave - but I have to admit that I really don't understand `select` or the underlying hardware primitives enough to make a solid case either way here. Maybe you can provide an equivalent program in a language of your choice that terminates - that would certainly prove that it's at least possible (though to be clear: I don't understand your xC code, so I can't promise that I'd understand whatever you send here, personally :) ).
 
All of that being said: I really think that in the cases where a priority select is needed, this construct is good enough to hold up. 

Hmm.. I must admit I smile here. I kind of like that we come to different conclusions. Would you you send your daughter with a fly by wire airplane that has some "good enough" sw?

This is… a strange question. I'm not saying "the software might be buggy, but it will be buggy software that is good enough". I'm saying "the construct is good enough to implement non-buggy software with it". Of course you need to know what the construct does and take it into account when writing your code - if you assume it works differently than it does, you'll introduce bugs, yes. That is true in any language.
 
There is a rather good explanation of PRI ALT (=pri select) of occam, where the TRUE & SKIP (=default) is also seen at page 72 of http://www.transputer.net/obooks/isbn-013629312-3/oc20refman.pdf.

Can you translate the example into that language? And demonstrate that it doesn't terminate?
 
It's not practical for me to learn a different language and scour its reference manual, to try and figure out how their select works, if that is compatible with how Go's select works and thus if and how lessons learned from that language are transferable to Go.

You seem to be convinced that their priority select is superior to Go's select construct and can map semantics that Go can't.

This is the only point I'll respond to tonight (with the possibility of being impolite since your response is so thorough). No, I am not saying that occam's PRI ALT or xC's [[ordered]] select are superior to Go's select (or Promela's :: for that sake: https://en.wikipedia.org/wiki/Promela#Executability). What I could say is that occam (and probably xC) nondeterministic choice (select without pri) is inferior to the Go select, since Go select is indeed pseudo random. I like it. My eager in telling about occam and xC prioritised choice may have mislead. I am simply saying that a select and a pri select are different, and I still don't understand why Go doesn't have pri select, and I started with asking about whether that had changed over the previous years. Compared with the letters "pri" the reflection case example is complicated, and the select default select.. special case for a small number of channels is simple to read, but in my opinion it stops there. I do want to be convinced that it is elegant (even if you promise it works, compared to "pri"), but I am not. About the case below. I like Go because it is so expressive, by the looks of it. When Go came with channels, you wont' believe what a relief if was to us who had used them and loved them for years. They were not dying. I don't have time to dig up an occam compiler (yes, they exist, there is some on comp.sys.transputer group, they even make FPGA transputers these days, for those who need them..) but doing what you suggest would have been fun. The semantics of an occam PRI ALT is simple and I programmed it for ten years, and I hope some of that understanding has remained.)

Øyvind

roger peppe

unread,
May 4, 2021, 7:13:51 AMMay 4
to Øyvind Teig, golang-nuts
On Mon, 3 May 2021 at 20:24, Øyvind Teig <oyvin...@teigfam.net> wrote:
I see that, which is great. But I still don't understand why https://go2goplay.golang.org/p/S_5WFkpqMP_H (By rog, 29Apr2021 23:52:05) seems not to print "Client 2".

That's because to try to emphasise the arbitrariness of the producers (and to check that the consuming code was working as intended), the producing code deliberately doesn't produce any values on channel 2. Instead, it sleeps for a second and produces a "late arrival" message.

It's trivial to take out the if statement and show that it works as you'd expect: https://go2goplay.golang.org/p/TjgiOmZtQhR

Øyvind Teig

unread,
May 5, 2021, 12:55:48 PMMay 5
to golang-nuts
mandag 3. mai 2021 kl. 23:12:03 UTC+2 skrev Øyvind Teig:
mandag 3. mai 2021 kl. 21:44:49 UTC+2 skrev axel.wa...@googlemail.com:
On Mon, May 3, 2021 at 9:16 PM Øyvind Teig <oyvin...@teigfam.net> wrote:
I don't see where hi and lo are being sent to?

They are being `close`d. Reading from a closed channel immediately succeeds, yielding the zero value.
I chose to use `close` because it's a non-blocking way to make a channel readable. You could get the same demonstration by making the channels buffered and writing to them.

I have played with that code at https://play.golang.org/p/U17AbmGBZUY. Instead of starting and starting lots of tasks, I just start 3. Sender (two configs: one with two tasks in parallel and one config with one task sending in sequence, and one with zero or 1 dim of the chans. However, I am just as wise. I don't see how any of the test cod" examples based on this proves that a hi is always taken if there is a lo lurking. I hope it's not my cognitive bias that I just cant' see it! I only see code that terminates if lo is taken after hi has been polled as not active some time before, but it says nothing about hi at the time lo is taken. It might have become active. If you see what any flawed thinking in the example above is, I'd be happy to know.

Here is my cognitive test case. It is not 100% realistic, but it's just to convey my point:
hiPri is a disconnect message of a smoke detetector. loPri is a fire alarm. If the disconnect hiPri is activated before (1 sec to 1 ns) or simulatenously (same polling of "hw pins") as the alarm loPri there should be no alarm being sent to the fire department. No emergency fire trucks. Not ever. There should not be any case of some low probability where this may happen. However, if the hiPri disconnect message was activated after the alarm, then it was too late, and the alarm will go off. (Disconnect was because we knew that there was going to be an alarm since we were doing something, and we explicitly did not want the fire trucks to come)

But then, "happens before" is "within a single gorotine". When it comes to several and concurrent goroutines there is no other order than those forced on them by synchronisation over nonbuffered channels (or the join you had in the code example).

Exactly. It is only *because* we use a single goroutine and we thus know that there is a happens-before edge.
For example, this program *also* exits (i.e. line 16 is executed), but you can no longer conclude that `hi` was ready when it does.

This is how most practical cases would likely happen.

I think my question was flawed, because really, the issue isn't about how the `select` with `default` construct we showed works - the question is how a priority `select` could work. That is, could we implement a priority `select` such that this code terminates: https://play.golang.org/p/4G8CY36L0Qy


I don't understand this question. Though I also see that my question is wrong - it should be "could we implement a priority `select` such that this code *never* terminates.

You used close which I didn't understand then. Thanks.
 
If we suppose a perfect priority select, which never chooses the `lo` case if the `hi` case is ready, the program I linked would never terminate.

Fair enough. But termination is not a requirement. That was just for the test code. The whole idea with a perfect priority select would, if we had it, to build the "fair algorithm" ourselves. For the disconnect and alarm cases there is only one message of each within some large time. The random select as in go might in case have caused the fire trucks to arrive when we had explicitly told not to.

The fair select (other than the random select of go, which I know the Go designers meant was "fair enough", or maybe even "fair" as they saw it) is when we have a pri select and can use it to make our own fair algorithm. No jamming, no starving. To repeat myself, the fair algorithm here would be to use a pri select and swap the order in the pri list.
 
Because `lo` is never ready unless `hi` is ready too. This, AIUI, is the priority `select` you'd want. Note, in particular, the made up `pri` keyword in the example.

Oh, if I could follow you on this one. I don't seem able to.
 
So, no, in that scenario we wouldn't have a pseudo-random choice. The question I was begging is if we could actually implement a select like you want. If you can write this program in another language, and that program ends up not terminating, that would demonstrate that it's indeed possible to write a `select` as you are requesting.

Commented the other day.
 
I don't think we can - and based on that assumption I extrapolated how a priority select would actually behave - but I have to admit that I really don't understand `select` or the underlying hardware primitives enough to make a solid case either way here. Maybe you can provide an equivalent program in a language of your choice that terminates - that would certainly prove that it's at least possible (though to be clear: I don't understand your xC code, so I can't promise that I'd understand whatever you send here, personally :) ).
 
All of that being said: I really think that in the cases where a priority select is needed, this construct is good enough to hold up. 

Hmm.. I must admit I smile here. I kind of like that we come to different conclusions. Would you you send your daughter with a fly by wire airplane that has some "good enough" sw?

This is… a strange question. I'm not saying "the software might be buggy, but it will be buggy software that is good enough". I'm saying "the construct is good enough to implement non-buggy software with it". Of course you need to know what the construct does and take it into account when writing your code - if you assume it works differently than it does, you'll introduce bugs, yes. That is true in any language.

Great. If expanded to "the construct is good enough to implement buggy-enough (per specification) software with it". Risk = probability * connsequence is never zero. Which is an argument against my alarm scenario. But it's the formally proven "never lo if hi" I am after.
 
There is a rather good explanation of PRI ALT (=pri select) of occam, where the TRUE & SKIP (=default) is also seen at page 72 of http://www.transputer.net/obooks/isbn-013629312-3/oc20refman.pdf.

Can you translate the example into that language? And demonstrate that it doesn't terminate?

If I were convinced that this is it, I could do it in xC. Would have been fun. But that would have to be in the course of this summer. But I must be convinced that it does what it should. My mentioned playing at  https://play.golang.org/p/U17AbmGBZUY didn't make it easier for me.

Øyvind