Go language: Introduction (II)

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

Go language: Introduction (II)

Before we get to the go language, let's start by filling in some basic concepts

Basic concepts

    • Concurrent Programs

A concurrent program can use multiple threads on a single processor or kernel to perform tasks, but only the same program runs on multicore or multiprocessor at some point in time for true parallelism.

Concurrent programs can be either parallel or not.

    • Multithreading Disadvantages

Using multithreaded applications is difficult to be accurate, the main problem is in-memory data sharing, they will be multi-threaded in an unpredictable manner, resulting in some can not reproduce or random results.

Using multithreading requires attention to synchronization issues, possible deadlocks, and the overhead of thread context switching

    • Large computational problem in single thread

The multi-core CPU is used to distribute computations to individual sub-processes, to decompose a large number of computations, and then pass the results through event messages between processes.

What is a co-process

    • Goroutines (co-process)

1. Association and thread Relationship

在协程和操作系统线程之间并无一对一的关系:协程是根据一个或多个线程的可用性,映射(多路复用,执行于)在他们之上的;协程调度器在 Go 运行时很好的完成了这个工作。

2. Co-process implementation

当系统调用(比如等待 I/O)阻塞协程时,其他协程会继续在其他线程上工作。**协程     的设计隐藏了许多线程创建和管理方面的复杂工作。**

3. Co-process cost

协程是轻量的,比线程更轻。它们痕迹非常不明显(使用少量的内存和资源):使用 4K 的栈内存就可以在堆中创建它们。因为创建非常廉价,必要的时候可以轻松创建并运行大量的协程(在同一个地址空间中 100,000 个连续的协程)。

What is a channel

Channels can only transmit one type of data, such as Chan int or Chan string, all types can be used for channels, and NULL interface interface{} is also possible. You can even (sometimes very useful) create channels for channels.

A channel is actually a queue of typed messages: the data is transferred. It is a first-out (FIFO) structure so that the order of the elements sent to them can be guaranteed (some people know that the channel can be likened to a bidirectional pipe (tw-way pipe) in Unix shells).

By default, communication is synchronous and unbuffered: The send does not end until the recipient receives the data. Imagine a unbuffered channel when there is no space to hold the data: A receiver must have the data ready to receive the channel and the sender can send the data directly to the receiver. So the Send/receive operation of the channel is blocked before the other party is ready.

Example:

package mainimport (    "fmt"    "time")func main() {    ch := make(chan string)    go sendData(ch)    go getData(ch)      time.Sleep(1e9)}func sendData(ch chan string) {    ch <- "Washington"    ch <- "Tripoli"    ch <- "London"    ch <- "Beijing"    ch <- "Tokio"}func getData(ch chan string) {    var input string    // time.Sleep(1e9)    for {        input = <-ch        fmt.Printf("%s ", input)    }}

Output:

Washington Tripoli London Beijing Tokio
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.