Don’t communicate by sharing memory. Communicate by sharing memory.
Go coroutines goroutine
What is a goroutine?
Goroutine is a coroutine in go.
So what is a coroutine?
Some people call a coroutine a lightweight thread, which means it consumes fewer resources.
Another question, what is a thread?
Threads, the smallest unit of CPU scheduling, are a mechanism that allows applications to execute multiple tasks concurrently.
So there’s another concept here, process, what is process?
A process is the smallest unit of resources allocated by the CPU. A running program is a process.
So now to sort out the relationship:
A process can contain more than one thread, a thread can contain more than one coroutine, and whether process, thread, coroutine is a way of program concurrent execution.
However, the biggest difference between coroutines and processes and threads is that the switching of coroutines is controlled by programs, while the switching of processes and threads is controlled by the operating system. Therefore, multi-process and multi-coroutines need to be scheduled by CPU during the switching, and their switching will consume more resources. Since the scheduling of coroutines is completely controlled by the user program, coroutines are more flexible and lightweight. A much better approach would be to implement concurrency using coroutines.
The use of goroutine
The use of Goroutine is very simple. You can turn an ordinary function into a coroutine by preempting it with the go keyword when it is called.
func main(a) {
for i := 0; i < 10; i++ {
go AFun(i)
}
time.Sleep(10 * time.Second)
}
func AFun(i int) {
fmt.Printf("fun%d begin... \n", i)
time.Sleep(1 * time.Second)
fmt.Printf("fun%d end.\n", i)
}
Copy the code
This enables the creation of 10 coroutines in parallel.
Coroutine communication is implemented using the channel channel
Coroutines coroutines, programs that cooperate, and that collaboration inevitably involves passing data to each other. In GO, channels are provided to pass data between goroutines.
A channel is like a queue. You can put data into a channel and then pull data out of the channel.
var g = make(chan int)
func routine1(a) {
g <- 10
fmt.Println("goroutine 1 done.")}func routine2(a) {
var1 := <-g
fmt.Println(var1)
fmt.Println("goroutine 2 done.")}func main(a) {
defer close(g)
go routine2()
go routine1()
Copy the code
The first line of this code creates a channel of type chan, which stores data of type int
The first line in the function Routine1
g <- 10
Copy the code
Pass 10 into the channel
And the routine2 function
var1 := <-g
Copy the code
Fetch the data from chan and store it in variables
In this way, a simple producer-consumer model is formed, and data transfer between coroutines is realized.
The type of the channel
ch2 := make(chan int.10)
Copy the code
With make, chan can be assigned cache, which is the maximum amount of data stored in chan. Such a Chan is called a cached Chan.
while
ch1 := make(chan int.0)
ch1 := make(chan int)
Copy the code
Is called chan without cache
There is also a type called one-way channel
// One-way channel
func foo1(out chan<- int) {
out <- 10
}
func foo2(in <-chan int) {
var1 := <-in
fmt.Println(var1)
}
Copy the code
Chan < – can only be saved
< – chan can take