Channel Basics
Channels are divided into read and write. If you use a lot of channels in the real world, please note that reading and writing channels anywhere in the code may cause different situations. Therefore, to avoid abusing channels in the team, I usually limit the situations in which I can only write and in which I can only read. If you mix them up, it will be very difficult to debug them, and also very difficult for the reviewer to read and understand.
It is very easy to distinguish between read and write, see where the <- symbol is placed, chan<- pointing to itself is write, <-chan leaving itself is read, it is quite easy to distinguish, if the func needs to use both read and write, then there is no need to use any arrow symbols, but I would suggest to split the logic of read and write into different func, it is very helpful for reading.
Communicating by sharing memory
In fact, there are similar examples in any language, so let’s use Go for example.
|
|
Please put the code directly into your computer to run, originally ints should be able to get 10 values normally, but you will find that the result is different every time. The reason is that the ints are declared as []int in the func, and the variable is shared in the goroutine, but it is possible to read and write to the same address memory at the same time, so you can see that the result is different every time. When using goroutine to read and write variables, try not to use share memory to share, sometimes it is really hard to debug the error.
The following are a few solutions, some of which are not suitable for use in the project. The first one is to modify GOMAXPROCS.
Originally, we would do parallel processing based on the number of CPUs, but we can set the number of CPUs to be used through GOMAXPROCS, so that the result will be as expected, but who would use GOMAXPROCS in the application? Another way is to use sync to solve it.
As long as the ints are read and written with Lock in front of them, the variables will not be overwritten and can be unlocked by using Unlock after writing. To make it simpler, you can use defer to Unlock, because it is dropped to goroutine to process in parallel, so you need to use WaitGroup to make sure all goroutines get the data before ending the func.
Share memory by communicating
The above example shows that sync.WaitGroup and sync.Mutex are used to complete the goroutine to get the correct data, if it is through the channel way can we avoid the problem mentioned above?
|
|
From the above example, we can see that there is only one for loop that can write to ints variables. This is what is called creating a channel as sharing memory by communicating, instead of throwing a variable into a goroutine for sharing and causing errors.
The goroutine is used first to write to the channel, so you can see that the first argument uses chan<- int to ensure that the goroutine can only write to the channel and cannot read data out of the channle. Then a for loop is used to read out the values from the channle one after another. With channel, there is no need for WaitGroup and Lock mechanism, just make sure to close the channle after the channel is finally read.
Benchmark
We benchmarked the above two implementations and the results are as follows:
You can find that the first way is actually better than using Channel by waitGroup + Lock.
Conclusion
The following two points can be summarized as the basis for my personal opinion:
- Unless you need to exchange messages between two goruoutines, it is better to use the usual waitGroup + Lock. Don’t force the use of channels in your project just because they’re trendy. In many cases, a normal Slice or callback will work better.
- As mentioned above, if you use a large number of goroutines and need to exchange data in the middle, you can use channels to communicate.