Thinking on concurrent parallel and goroutine of Golang

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

2015-04-14 WCDJ


In the concurrency of Golang, we discuss several problems of concurrency, respectively, as follows:

1,goroutine whether the concurrency problem
2,goroutine non-concurrency security issues
3, atomic operation problems in concurrent situations

Based on the above discussion, this paper further discusses the scheduling mechanism of goroutine and the implementation principle of concurrency.


0 Goroutine Light-weight Dispatch unit

Programmers using Goroutine can quickly develop a concurrent (possibly non-parallel) execution of a program. Goroutine is an implementation of the coprocessor (coroutine, lightweight process, light Weight process), and Linux POSIX C also provides coroutine system calls. The co-process (lightweight processes) is the execution unit that implements scheduling in user space, which is lighter than threads. The scheduling of the process is not preemptive, which means that the same thread can only actively abandon the CPU, the scheduler will be another waiting for the process of scheduling in and then execute. Goroutine is implemented in the run-time environment (runtime) of the Go language, and is dispatched between one or more threads within the user space (not confined to one thread). When a goroutine invokes a standard library function that might be blocked, the goroutine is suspended by the runtime environment, and another prepared Goroutine is dispatched and executed.

In principle, the scheduler in the Go Language runtime environment is not much different from the operating system scheduler, where the smallest scheduling unit is paused or started when an event occurs, but the Goroutine schedule is executed within the user space, so scheduling is faster, And can support a large number of concurrent goroutine run concurrently.

The go language is a fully compiled language (static language) whose code is executed directly by compiling the cost of the machine code, so there is no intermediate interpreter or virtual machine. To implement the functionality of the Goroutine runtime, the go language is accomplished using the runtime environment. When the Go program is linked, it will be added to a set of runtime environments. This set of environments is similar to a static library that contains some of the runtime features specified in the Go language, such as garbage collection, Goroutine scheduling, and so on. In the Go Language standard library, in each potentially blocking function, the scheduler in the runtime environment is eventually notified, allowing the scheduler to pause the current goroutine so that the design can greatly simplify the development of concurrent (possibly non-parallel) programs.


1 Goroutine non-blocking calls with Lost Select/poll/epoll

In the go language, do you provide a select/poll/epoll function similar to the C language? How do I make a network connection use non-blocking I/O? How do I asynchronously read and write to a network connection?

The answer to these questions is: Go Network program does not use asynchronous operation, all operations are synchronous, each goroutine processing a connection.

At the go code level, developers see the use of goroutine for blocking read and write, and in the internal implementation of go, it is the use of asynchronous operations, through the goroutine of the scheduling to complete the event processing. As the smallest dispatch unit, Goroutine is executed concurrently (possibly non-parallel).






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.