In Go, what happens if you write to a private channel? Can I view channels as deterministic destruction of RE?

Well, SO warns me of a subjective name, so please let me explain. Right now I'm looking at Go, I read the spec, looked at some IO posts, it looks interesting, but I have some questions.

One of my favorite examples was this select statement, which was listening to a channel that came from "DoAfter ()" or something else, the channel would send something at a given time.

Something like this (this probably doesn't work, pseudo-go, if anything!)

to := Time.DoAfter(1000 * Time.MS) select: case <-to: return nil //we timed out case d := <-waitingfor: return d 

Suppose that the thing we are waiting for happens very quickly, so this function returns and no longer listens to what happens in DoAfter?

I like and know that you should not test the channel, for example

 if(chanToSendTimeOutOn.isOpen()) { chanToSendTimeOutOn<-true } 

I like the places where the channels are synchronized, while, for example, it is possible that the above function may return after the isOpen() test, but before sending the truth. I am really against the test, this avoids what the channels do - hide locks and much more.

I read the spec and saw a panic and recovery at runtime, but in this example, where are we recovering? Is a thing waiting for a timeout to be sent to a routine program or kind of “object”? I imagined this “object” that had a sorted list of things that it had to send after given times, and that it simply adds TimeAfter requests to the queue in the correct order and passes through it. I don’t know where it will turn out in order to actually recover.

If he spawned go-routines, each with its own timer (controlled by runtime, of course, so that threads do not actually block time), then what will get a chance to recover?

Another part of my question relates to the lifetime of channels, I would suggest that they are recounted, and those who can read are recounted, so if there is no readable link anywhere, it is destroyed. I would call it deterministic. For the "point-to-point" topologies that you can form, it will be, if you stick to the "Go", send the material through the channels, do not contact it. "

So, for example, when a thing that wants a timeout returns the channel to no longer read by anyone. So, routine is now pointless, is there a way to get it back without work?

Example:

A file reader that uses defer to close a file when it is done, can it "sense" the channel that is supposed to send the material, was closed and thus return without reading?

I would also like to know why the select statement is "non-deterministic", I would really like it if the first case took precedence, if the first and second are ready (for a non-blocking operation) - I do not blame for this, but is there a reason? What is the implementation of this?

Finally, how are running routines planned? The compiler adds some kind of "compliant" every so many instructions, so the thread will switch between different goroutines? Where can I find information about lower level materials?

I know that Go touts “you just don’t need to worry about it,” but I like to know what I am writing is actually hiding (it could be C ++) and the reasons why.

+6
source share
1 answer

If you write to a private channel, your program will panic (for example, http://play.golang.org/p/KU7MLrFQSx ). You could catch this error with recover , but in a situation where you don’t know if the channel you are writing is open, it is usually a sign of an error in the program. The channel sending side is responsible for closing it, so it must know the current state. If you have several goroutines sent over the channel, then they should coordinate when closing the channel (for example, using sync.WaitGroup ).

In your Time.DoAfter hypothesis, the hypothesis will depend on whether the channel has been buffered. If it was an unbuffered channel, then the goroutine writing to the timer channel would be blocked until someone reads the channel. If this does not happen, gorutin will remain blocked until the program ends. If the channel has been buffered, sending will end immediately. A channel can be garbage collected before anyone reads it.

The standard time.After library behaves this way, returning a channel with a single slot.

+8
source

Source: https://habr.com/ru/post/970501/


All Articles