"Go language Programming"-concurrent programming

Source: Internet
Author: User
Tags least privilege
This is a creation in Article, where the information may have evolved or changed.

Organized from Go language programming-the fourth chapter

1. Concurrency Basics

Multi-process : Multi-process is the basic mode of concurrency at the operating system level. It is also the most expensive mode. On the Linux platform, it is the tool chain that uses this mode to work. For example, a Web server, it will have a dedicated process responsible for network port monitoring and link management, there will be specialized processes responsible for transactions and operations. The advantage of this approach is that it is simple, non-trivial, and the downside is that the system is expensive because all processes are managed by the kernel.
Multithreading : Multithreading is a system-level concurrency pattern on most operating systems, and is the most efficient mode we use most. For now, almost all of the toolchain we see will use this pattern. It is much less expensive than multi-process, but its overhead is still relatively large, and in high concurrency mode, efficiency can be affected.
Callback-based non-blocking/asynchronous IO: The emergence of this architecture actually originates from the crisis of the multi-threading model. In many highly concurrent server development practices, the use of multithreaded mode can quickly deplete the memory and CPU resources of the server. This mode uses asynchronous IO in an event-driven way, the server continues to run, and as few threads as possible, reducing overhead, it is now well practiced in node. js, but using this pattern, programming is more complex than multithreading, because it splits the process and does not respond naturally to the problem itself.
co-process (coroutine) is essentially a user-state thread that does not require an operating system for preemptive scheduling and is stored in a thread in a real implementation, so the system overhead is minimal, which can effectively improve the task concurrency of threads and avoid the disadvantage of multithreading. The advantage of using the process is that the programming is simple, the structure is clear, the disadvantage is that language support is required, if not supported, the user should implement the scheduler in the program itself.

2. Co-process

Execution is an abstract concept, with multiple concepts at the operating system level, such as the operating system's own processes (process), in-Process threads (thread), and in-process coroutine (also called lightweight threading)
Most languages do not directly support co-processes at the syntactic level, but are supported through libraries.
The Go language supports lightweight threading at the language level, called Goroutine.

3, Goroutine

Goroutine is a lightweight threading implementation in the Go language, managed by the Go Runtime (runtime).
. Go: keyword, before the function call plus the GO keyword, this call will be executed concurrently in a new goroutine. This goroutine also ends automatically when the called function returns. The return value of this function will be discarded if it is not received using the channel.

4. Concurrent Communication

No matter what the platform, what programming language concurrency is a big topic.
In engineering, there are two most common types of concurrent communication models: shared data and messages.
Shared data: Multiple concurrent units hold a reference to the same data, respectively, to share the data. The data being shared may have many forms, such as memory blocks, disk files, network data, and so on. Shared memory is the most common in real-world engineering applications.
Go language Community: Do not use shared memory to communicate, but should communicate to share memory.
The Go language provides an alternative communication model, which is to use message mechanisms rather than shared memory as a means of communication.

5. Channel

(1) Concept
The Channel is the way that the go language communicates between the goroutine provided at the language level. We can use the channel to pass messages between two or more goroutine.
Channel is an in-process communication mode, so the process of passing objects through the channel and calling the function parameters
Transitive behavior is consistent, such as the ability to pass pointers and so on.
The channel is type-dependent, and a channel can pass only one type of value.

(2) Basic grammar
Declaration form:
Var Channame Chan ElementType
Example: Var ch chan int
Define channel:
Ch: = make (chan int)
Write data to the channel:
Ch <-Value
To read data from the channel:
Value: = <-Ch

(3) Select
The Go language supports the SELECT keyword directly at the language level to handle asynchronous IO issues.
Select {
Case <-Chan1:
If Chan1 successfully read the data, the case processing statement
Case Chan2 <-1:
If the data is successfully written to chan2, the case processing statement
Default:
If none of the above is successful, go to the default process
}

(4) Buffering mechanism
Create a channel with a buffer:
C: = Make (chan int, 1024)

(5) Timeout mechanism
In the concurrent programming communication process, the most need to deal with is the time-out problem, that is, when writing data to the channel to find that the channel is full, or from the channel to try to read the data when the channel is found empty. If this problem is not handled correctly, it may cause the entire goroutinue to be locked.
The go language does not provide a direct time-out mechanism, but the select mechanism can be used to solve the timeout problem.

(6) Transmission of channel
Note: In the Go language, the channel itself is a native type, as is the case with map types, so the channel itself can be passed through CHANNL after its definition.

(7) One-way channel
One-way channel can only be used to send or receive data.
From the designer's point of view, all code should follow the principle of "least privilege"
(8) Close channel
Built-in functions using the Go language: Close (CH)
The use of multiple return values when reading can be judged by: x, OK: = <-ch

6. Let time slice

Each goroutine controls when the active time slice is given to other goroutine, which can be implemented using the gosched () function in the runtime package.

7. Synchronization

The sync Pack in the Go language pack offers two types of lock: Sync. Mutex and sync. Rwmutex.

8. Global Uniqueness Operation

The go language provides a once type to guarantee global uniqueness operations.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.