Go learn it from scratch concurrent (2): The channel

Since goroutine shared memory resource, in order to avoid problems such as mutex to ensure data accuracy, introducing the concept of channels (channel)

Go language channel (channel) is a special type. At any time, only one of them goroutine access channel to send and retrieve data. Between goroutine can communicate through the channel.

A conveyor belt or like channel queue, always follow the FIFO (First In First Out) rules guarantee the order of the data transceiver.

 

statement:

var type channel chan variable name

var A Chan int // a channel is int

Null chan type is nil, in order to make use of the need to meet after the statement.

 

create:

Channel is a reference type, you need to make use of to create, in the following format:

Examples channel name: = make (chan type)

var c2 chan int
c1 := make(chan interface{})
c2 = make(chan int)

 

send data:

Variable Channel <- Value

C1: = the make (Chan interface {}) 
CH <- 0 // 0 into the passageway, otherwise an error in need

When data is transmitted to the channel, if the recipient has not received, the transmission operation will continue to clog. Check the program can find some never sent successfully statements Go program is running and make prompt.

main FUNC () { 
 CH: = the make (Chan interface {})
  // 0 is placed in the channel 
 CH <- 0 
 // The hello string in the channel 
 CH <- " hello " 
 fmt.Print (CH) 
}

operation result:

fatal error: all goroutines are asleep - deadlock!

goroutine 1 [chan send]:

main.main ()

D:/SoloWork/Learn/main.go:8 +0x76

 

Receive data:

Transceiving operation is performed in the channel between two different goroutines, since the data is not received in the channel side processing, data transmission side will continue to clog

Will continue receiving the transmission data block until the transmission side, if the recipient receives the channel data is not sent by the sender, the receiver blocking occurs, which transmit data until the transmission side until

Channel can receive only one data element.

 
Blocking receive data

Reception value: = <- Channel name

data := <-ch
 
Non-blocking receive data

Received value, it is determined whether the channel has been closed: = <- Channel name

data, ok := <-ch

When the channel is closed to false ok

 
Receive any data, ignored data received
<-ch

Blocking will occur when the statement is executed until it receives the data, but the data received will be ignored.

This approach is mainly used for synchronization between channels concurrency goroutine blocking transceiver.

 
Receiving loop

The data channel received by the receiving operation can be borrowed for range statement plurality of elements

for data := range ch {}

Ch is the channel can be traversed, traversing the result is the received data. Data type is the data type of channel. Variables for traversing get only one, namely data

 

Close passage:

close (channel name)

Mainly used to terminate a range cycle.

Only the sender can close the channel, and the receiver can not

The sender may () Close a channel represented by a value close to no need to send the recipient assigned by the expression for the received second parameter to test whether a channel is closed

 

Way channel:

var channel instance chan <- element type // only transmission channels 
var channel example <-chan element type // only receiving channel

A one-way channel is not a channel filled with data (send) can only be read, most of the time is meaningless, mainly for rigor to ensure the interface code

 

Buffer channel:

The length of the buffer as the second parameter provided to the  make channel buffer is initialized with:

Examples channel name: = make (chan type, buffer length)

ch := make(chan int, 100)

When the channel has a buffer, only when the channel buffer is full, data will be transmitted thereto obstruction. When the buffer is empty, the recipient will be blocked.

The buffered channel is transmitted without waiting for the transmission to complete the recipient receives the process, and no blocking occurs only when the memory space is full will jam occurs. Similarly, if the channel data buffer, blocking does not occur when the received data is not read until the channel, the channel will be blocked again. (Corresponding to the initial amount of the mutex inside the operating system)

Unbuffered channel length can be seen as a buffered channel is always 0 (i.e., blocked into data transmission is started, equivalent to a mutex)

 

Guess you like

Origin www.cnblogs.com/VingB2by/p/11119877.html