Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of Goroutine Unblocking on Single Channel

Tags:

go

channel

Does order in which the Goroutines block on a channel determine the order they will unblock? I'm not concerned with the order of the messages that are sent (they're guaranteed to be ordered), but the order of the Goroutines that'll unblock.

Imagine a empty Channel ch shared between multiple Goroutines (1, 2, and 3), with each Goroutine trying to receive a message on ch. Since ch is empty, each Goroutine will block. When I send a message to ch, will Goroutine 1 unblock first? Or could 2 or 3 possibly receive the first message? (Or vice-versa, with the Goroutines trying to send)

I have a playground that seems to suggest that the order in which Goroutines block is the order in which they are unblocked, but I'm not sure if this is an undefined behavior because of the implementation.

like image 430
Justin Avatar asked Sep 16 '14 04:09

Justin


People also ask

What happens when you block a goroutine in go?

You read right, when you write on a channel the goroutine will usually block, if before the data is read the channel is closed the blocked goroutine will panic. This does not happen when you read from a channel, in Go you can read it and find out whether it was closed (either before reading or while blocked reading).

How to get the output of a goroutine from a channel?

So once the goroutine hello () complete its execution and write “true” to the channel then in the main it will able to read from the channel and executes further. Thus the output will be:– Thus instead of using time.sleep (), we can achieve it using channels and this is one of the best way to resolve it.

What is a goroutine in Go programming language?

For Concurrency in Go we use Goroutines and channels. They are very similar to threads as used in other languages and also run concurrently with other functions or methods. Let’s see how a Goroutine looks like. Here we have just seen how a goroutine looks like. But to make it work we have to modify it little bit.

What is the difference between GO channel select and go waitgroup?

Compared to go waitgroup, the go channel select works differently. It is used to choose from multiple send/receive channel operations and it blocks until one of the send/receive operation is ready You will get a deadlock, when you are trying to read from a channel, but none is writing to that channel.


3 Answers

This is a good question - it touches on some important issues when doing concurrent design. As has already been stated, the answer to your specific question is, according to the current implementation, FIFO based. It's unlikely ever to be different, except perhaps if the implementers decided, say, a LIFO was better for some reason.

There is no guarantee, though. So you should avoid creating code that relies on a particular implementation.

The broader question concerns non-determinism, fairness and starvation.

Perhaps surprisingly, non-determinism in a CSP-based system does not come from things happening in parallel. It is possible because of concurrency, but not because of concurrency. Instead, non-determinism arises when a choice is made. In the formal algebra of CSP, this is modelled mathematically. Fortunately, you don't need to know the maths to be able to use Go. But formally, two goroutines code execute in parallel and the outcome could still be deterministic, provided all the choices are eliminated.

Go allows choices that introduce non-determinism explicitly via select and implicitly via ends of channels being shared between goroutines. If you have point-to-point (one reader, one writer) channels, the second kind does not arise. So if it's important in a particular situation, you have a design choice you can make.

Fairness and starvation are typically opposite sides of the same coin. Starvation is one of those dynamic problems (along with deadlock, livelock and race conditions) that result perhaps in poor performance, more likely in wrong behaviour. These dynamic problems are un-testable (more on this) and need some level analysis to solve. Clearly, if part of a system is unresponsive because it is starved of access to certain resources, then there is a need for greater fairness in governing those resources.

Shared access to channel ends may well provide a degree of fairness because of the current FIFO behaviour and this may appear sufficient. But if you want it guaranteed (regardless of implementation uncertainties), it is possible instead to use a select and a bundle of point-to-point channels in an array. Fair indexing is easy to achieve by always preferring them in an order that puts the last-selected at the bottom of the pile. This solution can guarantee fairness, but probably with a small performance penalty.

(aside: see "Wot No Chickens" for a somewhat-amusing discovery made by researchers in Canterbury, UK concerning a fairness flaw in the Java Virtual Machine - which has never been rectified!)

like image 67
Rick-777 Avatar answered Nov 13 '22 18:11

Rick-777


I believe it's unspecified because the memory model document only says "A send on a channel happens before the corresponding receive from that channel completes." The spec sections on send statements and the receive operator don't say anything about what unblocks first. Right now the gc toolchain uses an orderly FIFO queue to control which goroutine unblocks, but I don't see any promises in the spec that it must always be so.

(Just for general background note that Playground code runs with GOMAXPROCS=1, i.e., on one core, so some types of concurrency-related unpredictability just won't come up.)

like image 27
twotwotwo Avatar answered Nov 13 '22 17:11

twotwotwo


The order is not specified, but current implementations use a FIFO queue for waiting goroutines.

The authoritative document is the Go Memory Model. The memory model does not define a happens-before relationship for two goroutines sending to the same channel, therefore the order is not specified. Ditto for receive.

like image 26
Simon Fox Avatar answered Nov 13 '22 17:11

Simon Fox