Traditional Culture Encyclopedia - Travel guide - Go CSP concurrency model

Go CSP concurrency model

Go's CSP concurrency model

Go implements two forms of concurrency. The first one is generally recognized by everyone: multi-threads share memory. In fact, it is multi-threaded development in languages ????such as Java or C++. The other one is unique to Go language and recommended by Go language: CSP (communicating sequential processes) concurrency model.

CSP is the abbreviation of Communicating Sequential Process. It can be called Communicating Sequential Process in Chinese. It is a concurrent programming model proposed by Tony Hoare in 1977. Simply put, the CSP model consists of concurrently executing entities (threads or processes). Entities communicate by sending messages. The channel used when sending messages is a channel, or channel. The key to the CSP model is to focus on the channel, not the entity sending the message. Go language implements part of CSP theory.

"Don't communicate by sharing memory. On the contrary, share memory through communication."

Go's CSP concurrency model is through goroutine and Channel is implemented.

Goroutine is the concurrent execution unit in Go language. In fact, it is a coroutine.

Channel is the communication mechanism before each concurrent structure (goroutine) in the Go language. In layman's terms, it is the "pipeline" for communication between goroutines, which is somewhat similar to the pipes in Linux.

Channel

Goroutine