Concurrency - lightweight threads, channel, single channel

1, a lightweight thread

goroutine Go language is lightweight threads implementation, when run by the Go (runtime) management. Go program will intelligently goroutine tasks reasonably assigned to each CPU.
Go program () function starts from main main pack, when the program starts, the program will Go () function creates a default goroutine for the main.

1) Use an ordinary function to create goroutine

Go used in the program  go  keyword to create a goroutine as a function. A function can be created multiple goroutine, a goroutine must correspond to a function.

Goroutine to create a common function worded as follows:

go function name (parameter list)

  • To call the function name: function name.
  • Parameter List: call functions need to pass parameters.

When you create a goroutine use go keyword, the called function's return value is ignored.

To return goroutine data by the data channel as the return value from the outgoing goroutine.

2) use an anonymous function to create goroutine

Go after keyword can also start goroutine anonymous function or closure.

When you use anonymous functions or closures create goroutine, in addition to the function definition section written on the back go outside, but also need to add parameters to call the anonymous function in the following format:

go func (parameter list) {
    function body
} (call parameter list)

among them:

  • Parameter List: a list of body function parameter variables.
  • Function body: Code anonymous function.
  • Call parameter list: When you start goroutine, you need to call the anonymous function parameter passing.

 

2, the channel

Go language instead of promoting the use of shared memory communication method, when a resource needs to be shared among goroutine, goroutine channel set up between a pipe, and provides a synchronization mechanism to ensure the exchange of data. When you declare a channel, you need to specify the type of data to be shared. Channel can be shared by the built-in type, the type name, the type and structure of a reference value or a pointer type.

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 FIFO rules guarantee the order of the data transceiver.

1) Channel Type Statement

Channel itself needs a type of modification, you need to identify the type of slice like the element type. Channel element type is the type of data transmitted therein, the following statement:

var channel chan variable channel type

  • Channel Type: the type of data in the channel.
  • Channel variables: Save the variable channel.

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

2) Create a channel

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

Channel instance: = make (chan data type)

  • Data type: type of element within the transmission channel.
  • Channel Example: channel handle created by make.

3) using the data transmission channels

Transmission channel using a special operator <-, the data format is transmitted through the channel: Channel Variable <- Value

  • Channel variables: create a good channel instance by make.
  • Found: can be a variable, a constant, expression or function return values. It must match the type of the value ch channel element type.

When data is transmitted to the channel, if the recipient has not received, the transmission operation will continue to clog.

4) using the received data channel

Channel receiver use the same <-operator, the receiving channel has the following characteristics:

  •  Transceiving operation is performed between different channels of two goroutine. Since the data channel in the absence of the receiving side processing, data transmission side will continue to clog, so the channel must be received in another of goroutine.
  • It 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 recipient block will also occur until the sender sends data.
  • Each time a receiving element. Channel can receive only one data element.

Data channel received four kinds of writing:

Blocking receive data:

Blocking mode to receive data, the receive variables as <-the left the value of the operator, the following format:

data := <-ch

It will be blocked when the statement is executed until the data is received and assigned to the variable data.

 

Non-blocking receive data:

Use nonblocking when receiving data from the channel, the statement does not occur clogging, the following format:

data, ok := <-ch

  • data: indicates that the received data. When data is not received, data of zero value channel type.
  • ok: it indicates whether the received data.

The method of receiving channel non-blocking may cause a high CPU occupancy, the use of very small. If you want a receiver timeout detection can be performed with the channel select and timers.

 

Receive any data, ignored data received:

After blocking receive data ignore the data returned from the channel, in the following format:

<-ch

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

 

Reception cycle:

Receiving data channel can be borrowed for range statement plurality of elements receiving operation, the following format:

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. Traversing variables obtained for only one, i.e. in the above example data.

 

3, the one-way channel

Go language's type system provides a one-way channel type, by definition, only one-way channel for transmitting or receiving data. channel itself must be to support reading and writing, or simply can not use.
If a channel can only really read, then it is surely only be empty because you have no chance to write the data entered. Similarly, if a channel is only allowed to write, even if written into, and has not the slightest sense, because there is no chance to read the data inside. The so-called one-way channel concept, in fact, except for one channel of restrictions.

Statement 1) in the unidirectional channel

We passed to a function to a channel variable may be specified by a variable one-way channel, thereby limiting the function of this channel may be operated in.

The channel variable declaration is very simple way, only transmission of channel type chan<-, channel type can only be received as <-chanthe following format:

Examples var channel chan <- // only send channel element type
var channel example <-chan only receive channel element type //

  • Element type: type element comprising the channel.
  • Channel Example: Channel variable declarations.

2) close the channel

Close channel is very simple, direct use of the built-in language Go close () function can be: close (ch)

 

 

 

 

Guess you like

Origin www.cnblogs.com/ACGame/p/12005757.html