In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to understand the go concurrency mechanism and the CSP concurrency model it uses". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand the go concurrency mechanism and the CSP concurrency model it uses.
1. Concurrency and parallelism
When it comes to concurrency, we have to mention parallelism. Here we review the differences and connections between concurrency and parallelism:
Concurrency: two or more tasks are executed within a period of time. We don't have to pay attention to whether these tasks are executed at the same time, maybe at the same time, or not. We only care about whether two or more tasks are solved in a short period of time, even in a very short time (one or two seconds).
Parallellism: two or more tasks are executed at the same time
In short, concurrency is a logical concept, while parallelism emphasizes the physical state of operation. Concurrency includes parallelism.
A brief introduction to CSP concurrency Model 2.1.CSP of 2.Go
In computer science, Communication Sequential process (communicating sequential processes,CSP) is a formal language for describing interaction patterns in concurrent systems. It is a member of the family of concurrent mathematical theories. It is called process algorithm (process algebras), or process Computing (process calculate). It is a mathematical theory based on message channel delivery.
The CSP model was proposed in the 1970s. Different from the traditional multi-thread communication through shared memory, CSP pays attention to "sharing memory by means of communication". CSP is a concurrency model used to describe the communication between two independent concurrent entities through shared communication, namely channel (pipes). Channel is the first kind of object in CSP. CSP does not care about the entity that sends the message, it only cares about the channel used when sending messages between entities.
Channel in Go is a structure that is created separately and can transmit messages between processes. Its communication pattern is similar to boss-worker mode, in which one entity sends messages to channel, and then another entity listens and receives messages from channel. In this process, the two entities are anonymous, which realizes the decoupling between entities.
CSP concurrency Model of 2.2.Go
Go implements two ways of concurrency. The first is widely recognized by everyone: multi-thread shared memory. It is actually multithreaded development in languages such as Java or C++. The other is specific to the Go language and recommended by the Go language: CSP concurrency model.
Ordinary thread concurrency models, such as Java, C++, or Python, communicate between threads by sharing memory.
A very typical way is to access shared data (such as arrays, Map, or some structure or object) through locks.
Therefore, in many cases, a convenient data structure is derived, called "thread-safe data structure". For example, the data structure in the package "java.util.concurrent" provided by Java.
The traditional thread concurrency model is also implemented in Go, such as sync.Map for the safe use of map,Go in concurrency scenarios.
Go's CSP concurrency model is implemented through goroutine and channel.
Goroutine is the concurrency execution unit in the Go language. It's a little abstract, which is similar to the traditional concept of "thread" and can be understood as "thread".
Channel is the communication mechanism before each concurrent structure (goroutine) in the Go language. Popularly speaking, it is the "pipeline" of communication between various goroutine, which is similar to the pipeline in Linux.
The underlying layer of goroutine uses coroutine to achieve concurrency. Coroutine is a user thread that runs in user mode, similar to greenthread.
Go chose to use coroutine because:
User space, avoiding the cost caused by the switching between kernel mode and user mode
Can be scheduled by the language and framework layer
Smaller stack space allows for the creation of a large number of instances
The way to generate a goroutine is very simple:
Go f ()
The communication mechanism channel is also very convenient, using channel to transmit data.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.