Go Language Learning Notes 3 – Concurrent primitives for Go

An overview of the

The three consecutive sections are as follows:

  • The first section covers basic syntax and data structures
  • Section 2 discusses methods and interfaces
  • The third section briefly introduces the concurrency primitives of Go.

Go ride

Go programs (Goroutines) are lightweight threads managed by the Go runtime.

Go the function name

A new Go procedure is launched and the function is executed. For example: Go say(“world”) // a thread will be started to execute say

The word “thread” here is not exact, but it’s easy to understand.

The Go programs run in the same address space, so they must be synchronized when accessing shared memory. The Sync package provides this capability, but it’s not often used in Go because there are other ways to do it.

func say(s string){ for i:=0; i<5; i++ { //fmt.Println(time.Millisecond) time.Sleep(100 * time.Millisecond); fmt.Println(s,i) } } func main() { go say("jack") say("lucy") }Copy the code

channel

Channels are pipes with types through which you can send or receive values with the channel operator <-.

Ch < -v // send v to channel CH. V := <-ch // Receives the value from ch and assigns v.Copy the code

(The “arrow” is the direction of the data flow.)

Like maps and slicing, channels must be created before use:

ch := make(chan int)
Copy the code

By default, both send and receive operations block until the other end is ready. This allows the Go program to synchronize without explicit locks or race variables.

Example:

func su(ch chan int){
  ch <- 1
  ch <- 2
}

func main() {
  ch := make(chan int)
  go su(ch)
  x := <- ch
  y := <- ch
  fmt.Println(x,y)
}
Copy the code

Buffered channel

Channels can be buffered. Initialize a buffered channel by supplying the buffer length as the second argument to make:

ch := make(chan int, 100)
Copy the code

Sending data to a channel blocks only when its buffer is full. When the buffer is empty, the receiver blocks.

Func main() {ch := make(chan int,2) ch < -1 ch < -2 When it writes again, it blocks. x := <- ch y := <- ch fmt.Println(x,y) }Copy the code

The range and close

The sender can close a channel with close, which indicates that there are no values to send. The receiver can test whether the channel is closed by assigning a second parameter to the receive expression: if no value can be received and the channel is closed, the execution is complete

v, ok := <-ch
Copy the code

Ok will then be set to false.

Write a loop like this:

For I := range c // Will receive values from the channel until it is turned off.

Note:

  • Only the sender can close the channel, not the receiver.
  • Sending data to a closed channel causes a crash. Note:
  • Channels, unlike files, usually do not need to be closed.
  • Closing is necessary only if the receiver must be told that there are no more values to send, such as terminating a range loop.

The select statement

The SELECT statement enables a Go program to wait for multiple communication operations. Select blocks until a branch can continue, at which point the branch is executed. When multiple branches are ready, one is randomly selected for execution.

Default selection The default branch is executed when no other branch in the SELECT is ready.

To avoid blocking when trying to send or receive, use the default branch:

Select {case I := <-c: // use I default: //Copy the code

sync.Mutex

We have seen that channels are very good for communication between Go programs. But what if we don’t need to communicate? For example, what if we just wanted to ensure that only one Go program could access a shared variable at a time to avoid collisions? The concept involved here is called mutualexclusion *, and we typically use a data structure called Mutex to provide this mechanism.

The SYNc. Mutex Mutex type and its two methods are provided in the Go library:

  • Lock
  • Unlock

We can ensure mutually exclusive execution of a block of code by calling Lock before code and Unlock after code. We can also use the defer statement to ensure that the mutex is unlocked.

Learn more

docscn.studygolang.com/doc/

go-zh.org/#

tour.go-zh.org/list

END