This is a creation in
Article, where the information may have evolved or changed.
Golang the most attractive place may be concurrency, regardless of code writing, or performance above, Golang has the absolute advantage
Learning a language concurrency feature, I like to implement a producer consumer model, this model is very classic, suitable for a lot of concurrent scenarios, below I through this model, to briefly introduce the Golang of the concurrent programming
Go concurrency syntax
Co-processgo
The Golang is the smallest unit of concurrency, similar to a thread in another language, except that the implementation of the thread is based on the implementation of the operating system, each time the scheduling of the thread is a system call, the need to switch from the user state to the kernel state, which is a very time-consuming operation, So too many threads in a common program can cause a lot of performance costs to switch on the thread. And in the Golang internal implementation of this scheduling, the co-operation in this scheduling under the switch is very lightweight, hundreds of the process of running in a Golang program is a normal thing
Golang for concurrency, the syntax for initiating a process is very simple, using go keywords to
go func () { // do something}
Sync Signalsync.WaitGroup
Multiple co-processes can be sync.WaitGroup synchronized, which is similar to the amount of traffic inside Linux
var wg sync.WaitGroup // 申明一个信号量wg.Add(1) // 信号量加一wg.Done() // 信号量减一wg.Wait() // 信号量为正时阻塞,直到信号量为0时被唤醒
Channelchan
The channel can be understood as a message queue, the producer puts it in the queue, and the consumer takes it from the queue. The channel can be used to close close
ic := make(chan int, 10) // 申明一个通道ic <- 10 // 往通道里面放i := <- ic // 从通道里面取close(ic) // 关闭通道
Producer Consumer Realization
Defining Product Classes
This product class is defined according to the specific business requirements
type Product struct { name int value int}
Producers
If the stop flag is not false , continue to the channel inside product , after completion of the signal volume
func producer(wg *sync.WaitGroup, products chan<- Product, name int, stop *bool) { for !*stop { product := Product{name: name, value: rand.Int()} products <- product fmt.Printf("producer %v produce a product: %#v\n", name, product) time.Sleep(time.Duration(200+rand.Intn(1000)) * time.Millisecond) } wg.Done()}
Consumers
Constantly take product from the channel, and then do the corresponding processing, until the channel is closed, and the products inside is empty, the For loop will terminate, and this is what we expect
func consumer(wg *sync.WaitGroup, products <-chan Product, name int) { for product := range products { fmt.Printf("consumer %v consume a product: %#v\n", name, product) time.Sleep(time.Duration(200+rand.Intn(1000)) * time.Millisecond) } wg.Done()}
Main thread
var wgp sync.WaitGroupvar wgc sync.WaitGroupstop := falseproducts := make(chan Product, 10)// 创建 5 个生产者和 5 个消费者for i := 0; i < 5; i++ { go producer(&wgp, products, i, &stop) go consumer(&wgc, products, i) wgp.Add(1) wgc.Add(1)}time.Sleep(time.Duration(1) * time.Second)stop = true // 设置生产者终止信号wgp.Wait() // 等待生产者退出close(products) // 关闭通道wgc.Wait() // 等待消费者退出
Reprint please indicate the source
This article link: http://hatlonely.github.io/20 ...