This file contains the Implementation of Go Channels.
invariants:
// At least one of c.sendq and c.recvq is empty,
// Except for the case of an unbuffered channel with< span class= "s2" > a single Goroutine
// Blocked on it for both sending and receiving using a select statement,
// In which case the length of c.sendq and c.recvq is limited only by The
Size of the Select statement.
//
for buffered channels, also:
// C.qcount > 0 implies that c.recvq is empty.
c.qcount < C.dataqsiz implies that c.sendq is empty.
Import (
"Runtime/internal/atomic"
"unsafe"
)
- the data structure in the channel is Hchan{}
Chan How to initialize:
func Makechan (t *chantype, size int) *hchan
Memory allocation Method:
With no buffers
C = (*hchan) (MALLOCGC (hchansize, nil, true))
with buffers
C = (*hchan) (MALLOCGC (hchansize+uintptr(size) *elem.size, nil, true))
—————-
Lock (&c.lock)
if c.closed! = 0 {
unlock (&c.lock)
Panic(Plainerror ("Send on closed Channel"))
}
——————
Because there are locking steps in the Chansend, It is safe to read and write multiple goroutine.
The PipedInputStream class and the PipedOutputStream class are used to create pipeline traffic in the application.
A PipedInputStream instance object must be connected to a PipedOutputStream instance object to produce a communication pipeline.
( must use connect,in.connect (out) or out.connect (in), the effect is equivalent )
PipedOutputStream can write data to the pipeline,pipedintputstream can read pipedoutputstream data written to the pipeline, These two classes are primarily used to complete communication between threads.
The data is read from within the [Out,in] range and written to the [In,out] range
{----X x x x x x X----}
^ ^
| |
Out in
{x x x x--------X x x}
^ ^
| |
In out
In pipedintputstream , the default size of the buffer is:
private static final int default_pipe_size = 1024x768;
When writing data:
synchronized void receive (byte b[], int off, int len) throws ioexception
byte[], and offsets are calculated
Communication between goroutine communication and thread in Java