Home  >  Article  >  Backend Development  >  Is golang pipeline thread safe?

Is golang pipeline thread safe?

(*-*)浩
(*-*)浩Original
2019-12-30 14:48:505870browse

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.

channel in golang                                                                                                                                                                                                                                                          

#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 channel

Essentially 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn