Home >Backend Development >Golang >Is golang pipeline thread safe?
If thread safety is defined as allowing multiple goroutines to read and write at the same time, then golang's channel is thread safe. There is no need to lock when reading and writing the same channel concurrently.
#If you don’t use channel and use shared global variables, you need to lock it// synchornized 同步
// golang中的 sync包中有互斥锁
var lock sync.Mutex // mutex 互斥
lock.Lock() // 上锁
// 多个goroutine同时对相同的数据进行修改
lock.Unlock() // 解锁
Using synchronization lock concurrency efficiency will be very low
channel main Used for goroutine communication and solving the problem of the main thread waiting for the goroutine execution to end before exiting
basic concept of channelEssentially it is a FIFO data structure-queue
Thread safety, no locking requiredChannels have types, such as string channel chan string, which can only save string data
The above is the detailed content of Is golang pipeline thread safe?. For more information, please follow other related articles on the PHP Chinese website!