Sync packet and channel mechanism in Go language

Source: Internet
Author: User
Tags mutex

Article reprinted to: https://www.bytelang.com/article/content/A4jMIFmobcA=

It is very simple to implement concurrency in Golang, just add the keyword "Go" before the function that needs concurrency, but how to handle the synchronization and communication between different goroutine in the Go concurrency mechanism, Golang provides the sync packet and channel mechanism to solve this problem.

The Sync package provides basic synchronization primitives such as mutexes. Types other than Once and Waitgroup are mostly used for the underlying library routines. More advanced synchronization operations are performed through channels and communications.

Type Cond    func newcond (l Locker) *cond    func (c *cond) broadcast ()    func (c *cond) Signal ()    func (c *cond) W AIT () type Lockertype Mutex    func (M *mutex) Lock ()    func (M *mutex) Unlock () type Once    func (o *once) do (f func () ) Type Pool    func (P *pool) Get () interface{}    func (P *pool) Put (x interface{}) type Rwmutex    func (rw *rwmutex) L Ock ()    func (rw *rwmutex) rlock ()    func (rw *rwmutex) rlocker () Locker    func (rw *rwmutex) runlock ()    Func (rw *rwmutex) Unlock () type Waitgroup    func (WG *waitgroup) Add (delta int)    func (WG *waitgroup) done ()    Func (WG *waitgroup) Wait ()

  

And the sync in Golang is through sync. Waitgroup to achieve. Waitgroup: It implements a queue-like structure that can continue to add tasks to the queue, remove them from the queue when the task is complete, and, if the tasks in the queue are not fully completed, block by using the wait () function to prevent the program from continuing until all queue tasks are complete.

Waitgroup a total of three methods: ADD (delta int), done (), Wait (). Add: Adding or reducing the number of wait goroutine done: equivalent to add ( -1) Wait: Execute block until all Waitgroup number becomes 0

Specific examples are as follows:

Package main    Import (      "FMT"      "Sync"  )    var waitgroup sync. Waitgroup    func afunction (shownum int) {      fmt. Println (shownum)      Waitgroup. Done ()//task completed, number of tasks in the task queue-1, in fact. Done is. ADD ( -1)  }    func main () {for      I: = 0; i <; i++ {          waitgroup. ADD (1)//Each creation of a goroutine, the number of tasks in the task queue +1          Go afunction (i)      }      waitgroup. Wait ()//. Wait () This will block until all the tasks in the queue are ended and unblocked  }  

  

Online Example: https://www.bytelang.com/o/s/c/6z7UkvezTJg=

Usage scenarios:

The program needs to be concurrent, need to create multiple goroutine, and must wait for these concurrency to complete before continuing the next program execution. The Waitgroup feature is that wait () can be used to block until all tasks in the queue are complete without having to sleep for a fixed time. However, the disadvantage is that you cannot specify a fixed number of goroutine.

Channel mechanism:

Relative sync. Waitgroup, the use of channel practice in Golang synchronization is much simpler. The channel itself can be blocked by <-data transfer, channel is a built-in basic type in Golang, there are only 4 ways for channel operation:

Create channel (via make () function, including no cache channel and cache channel);

Adding data to the channel (Channel<-data);

Reading data from the channel (Data<-channel);

Close channel (implemented by the close () function, cannot save data to the channel after closing, but can continue to read data from channel)

The channel is divided into buffer channel and unbuffered channel, and the two channel creation methods are as follows:

var ch = make (chan int)//unbuffered channel, equivalent to make (chan int, 0)

var ch = make (chan int,10)//buffered channel, buffer size is 5

In which the unbuffered channel is blocked in both read and write, while the buffer channel is stored in the channel when the data is not reached the total number of channel buffers, it can be kept to the inside until the cache is full before blocking. Due to the presence of blocking, special attention is paid to using the channel to prevent the creation of deadlocks. Examples are as follows:

No cache channel:

Package Main Import"FMT"func afuntion (Ch Chanint) {fmt. Println ("Finish")      <-Ch} func main () {ch:= Make (chanint)//non-buffered channelgo afuntion (CH) ch<-1            //Output Result://Finish}

Online Example: https://www.bytelang.com/o/s/c/3cxH7Jko7YY=

Code Analysis: First create a non-buffered channel CH, and then execute Go afuntion (CH), at this time to execute <-ch, then afuntion this function will block, no longer continue to execute, until the main process ch<-1 to channel CH The afuntion is not unblocked by injecting data into the process.

Correct:

Code Analysis: For this program (only a single-core CPU running program) first create a non-buffered channel CH, and then encounter Go afuntion (CH), see that no CPU can be used to run the task, the task is noted, wait until there is a CPU, then run the task, and then execute CH <-1, at this time the main goroutine block, find whether there are other processes, find Afuntion (CH) This goroutine, then execute the goroutine content until <-ch from the main goroutine to obtain data 1, Remove the main goroutine block. (Note: This type of execution is limited to single-core CPUs)

If you specify multiple CPUs to run, first run the primary goroutine to create the unbuffered channel, and then see if there is an idle CPU that can run another goroutine, and if so, run the coprocessor afuntion (CH), for multicore CPUs, The order in which the main goroutine and the other goroutine is run is indeterminate.

package main
Import"FMT"Import"Runtime"Import" Time"func afuntion (Ch Chanint) {fmt. Println ("Finish")      <-Ch} func main () {runtime. Gomaxprocs (runtime. NUMCPU ()) Ch:= Make (chanint)//non-buffered channelgo afuntion (CH) time. Sleep (time. Nanosecond* +) fmt. Println ("Main Goroutine") Ch<-1  }  

 

Online Example: https://www.bytelang.com/o/s/c/9z_uWI5ZumA=

Operation Result:

Finishmain Goroutine

or main goroutine

Finish

The execution order of the primary goroutine and the other goroutine is indeterminate (for multicore CPUs)

Package Main Import"FMT"func afuntion (Ch Chanint) {fmt. Println ("Finish")      <-Ch} func main () {ch:= Make (chanint)//non-buffered channel//just swap the code order of the two lines.CH <-1go afuntion (CH)//Output Result://deadlock, no results}

Online Example: https://www.bytelang.com/o/s/c/sLL_Cto3k4E=

Code Analysis: First create a non-buffered channel, and then in the main process into channel CH through the ch<-1 command to write data, then the main process block, can not execute the following go afuntions (CH), nature will not be able to release the main process blocking state, The system deadlock

Summarize:
For non-cached channel, put the channel and the data from the channel to the outside of the two operations can not be placed in the same process to prevent the occurrence of deadlocks, and should first use go to open a process to operate the channel, at this time to block the go process, And then the channel in the main process of the opposite operation (and go to the channel to reverse the operation), to implement the Go Association threads unlocked. That is, the go process must be in front, unlocking the coprocessor at the back.

With Cache channel:
In the case of a cache channel, as long as the channel is not in the cache, the data can be deposited into the channel until the cache is full; As long as the channel cache is not 0, the data can be taken out of the channel until the channel cache becomes 0.

Thus, relative to the non-cache channel, with cache channel is not easy to cause deadlock, can be at the same time in a goroutine ease of use,

Close ():

Close is mainly used to close the channel channels, where it is close (channel), and where real producers are closing the channel, rather than closing it in the consumer's place. And when the channel is closed, it is no longer possible to continue to deposit data in the channel, but can continue to read from the channel. Examples are as follows:

Package Main Import"FMT"Func Main () {varch = make (chanint, -)       forI: =0; I <Ten; i++{ch<-i} close (CH)//CH <-//panic:runtime error:send on closed channel     forI: =Range Ch {fmt. Println (i)//Output 0 1 2 3 4 5 6 7 8 9    }  } 

Online Example: https://www.bytelang.com/o/s/c/XBiMiCoE7dc=

Channel Blocking timeout Processing:
Goroutine sometimes go into blocking situations, so how to avoid the blockage of the whole program due to channel blocking? Solution: Set timeout processing via Select, as follows:

Package main Import ("FMT"      " Time") Func main () {c:= Make (chanint) O:= Make (chanBOOL) go func () { for {              Select {               CaseI: = <-c:fmt. Println (i) Case<-time. After (time. Duration (3) * time. Second)://The set timeout is 3s, and if the channel 3s clock is not responding and is blocked, the report times out and the timeout is processed. Fmt. Println ("Timeout") o<-true                   Break              }          }      }()      <-O}

Online Example: https://www.bytelang.com/o/s/c/6V74LnkRLN0=

Golang Concurrency Summary:

Concurrency two ways: sync. Waitgroup, the best advantage of this approach is that wait () can block all tasks in the queue before it is unblocked, but its disadvantage is that the number of concurrent threads cannot be specified.
Channel Advantages: The ability to use a channel with a buffer to specify the concurrent Goroutine, more flexible. However, its disadvantage is that it is easy to create deadlocks if used improperly, and he also needs to decide whether or not the concurrent goroutine is executed.

However, the channel is more flexible, more convenient to use, and through the time-out processing mechanism can be very good to avoid the channel caused by the program deadlock, so the use of channel to achieve program concurrency, more convenient and easier to use.

Sync packet and channel mechanism in Go language

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.