Don't communicate by sharing memory Instead share memory by communicating
Netizens explained this sentence as follows:
这句俏皮话具体说来就是,不同的线程不共享内存不用锁,线程之间通讯用通道(channel)同步也用channel。
Chanel is a medium for transmitting information between coroutines, elegantly solves the lock, unlock, critical section, etc. that can be seen everywhere in some common back-end development languages, and moves the problems solved from many thread levels to coroutines, thereby statically Guaranteed to have no data races.
Channel declaration and creation
The pseudo code is as follows:
//声明类型
var 通道名 chan 数据类型
//创建通道
通道名 = make(chan 数据类型)
A practical example is as follows:
package main
import "fmt"
func main() {
var a chan int
fmt.Printf("%T, %v\n", a, a)
if a == nil {
a = make(chan int)
fmt.Printf("%T, %v\n", a, a)
}
}
The result of running is:
chan int, <nil>
chan int, 0x1400001a360
The channel is a memory address, which also shows that it is actually a reference type of data.
Receive & send data
For the same channel, its read data and write data are blocked .
The pseudo code is as follows:
//从通道读数据
data := <-a
//把数据写入通道
a <- data
A practical example is as follows:
package main
import "fmt"
func main() {
// 首先创建一个bool类型的通道
var ch1 chan bool
ch1 = make(chan bool)
//下面启动一个go routine
go func() {
for i := 0; i < 10; i++ {
fmt.Println("子goroutine中, i: ", i)
}
fmt.Println("completed")
//循环结束后 向团队中写数据,表示要结束了
ch1 <- true
}()
//在主程序中读取数据
data := <-ch1
//打印一下 我们读到的数据
fmt.Println("main data: ", data)
fmt.Println("main goroutine completed")
}
The results are as follows:
子goroutine中, i: 0
子goroutine中, i: 1
子goroutine中, i: 2
子goroutine中, i: 3
子goroutine中, i: 4
子goroutine中, i: 5
子goroutine中, i: 6
子goroutine中, i: 7
子goroutine中, i: 8
子goroutine中, i: 9
completed
main data: true
main goroutine completed
Our sub-goroutine prints 1~10 in a loop. After the printing is completed, write the ch1 of the chanel type as true,
At this time, the main goroutine can proceed to the next step according to this condition. Before that, in fact, even if the main goroutine grabbed the resource first and read data from ch1, but now there is nothing in the channel, it can only block, and then Obediently hand over resources to our child goroutine until the end of the loop and write true into ch1.
The following points should be noted:
- chanel is a nil type chanel that requires a specified type and cannot be used directly.
- The chanel itself is synchronous, and only one goroutine can operate at the same time.
- Chanel is used to transfer data between goroutines. The sending and receiving of chanel data must be in different goroutines. If only one goroutine does not use chanel, a deadlock will occur in this case.
- Reading data from chanel will be blocked immediately until there is a goroutine that writes data to chanel.
- Writing data to the channel will be blocked immediately until there is a goroutine that reads data from the channel.
(The above is relative to the channel without cache. The cache channel mentioned later will block when the buffer is full, rather than blocking immediately)
Reference: bilibili
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。