Go Language Learning-concurrency

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

Goroutine:

Lightweight threads managed by Go run environment

Channel

There is a type of pipe, the operator is the direction of the <-data flow arrow pointing

Use make (Chan buffer data type buffer length) to create

Close the pipe using Close (chan), only the sender is allowed to close the pipe

Cases

CH: = Make (chan int 10)

CH <-10

A: = <-ch

Or

A: = Int (0)

A = <-Ch

One-way pipelines: one-way pipelines can only be used to send or receive data.

CH1: = make (chan int)

CH2: = <-chan int (CH1)//CH2 is defined as a read-only channel

CH3: = chan<-int (CH1)//CH3 is defined as a channel that can only be received

Select

In order for a goroutine to be able to wait on multiple communication operations, you can use Select

Select blocks until the communication in a branch is ready to continue.

When more than one communication is ready, a random one is selected for communication.


Synchronous:

The sync must be used to lock, the sync package provides two kinds of locks: sync. Mutex and sync. Rwmutex.

A mutex is the simplest mutex.

Rwmutex is also a mutex, but when read, it blocks the write, but does not prevent it from being read.

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.