Golang-Channels channel blocking

Source: Internet
Author: User

The new Year has begun, no matter what happened before today, look forward, enough.

When it comes to channel, you must say a thread. Any actual project, regardless of size, concurrency is inevitable. The presence of concurrency involves thread communication. In the current development language, there are two main types of thread communication, shared memory and message passing. Shared memory must be familiar, enabling inter-thread communication by working together on the same object. Messaging is done in a similar way to chat. Golang uses a co-process technique for concurrent processing. The goroutine of Golang is the implementation of the association process. The concept of the association is very early, and simply understood as a lightweight thread, Goroutine is designed to solve the communication between concurrent tasks. Golang the idea of resolving communication is not to communicate through shared memory, but to share memory through communication. The Golang solution is a messaging mechanism, and the delivery of messages is done through channel.

The use of the channel is simple, and it is not something that sticks to others. Now talk about the understanding of Channe blocking L.

Sender angle: For the same channel, the send operation (in the coprocessor or function) is blocked until the receiver is ready. If the data in Chan is not received, no additional data can be passed to the channel. Because the new input cannot be passed in without the channel being empty. So the send operation waits for Chan to become available again: When the channel value is received (the variable can be passed in).

Receiver angle: For the same channel, the receive operation is blocked (in the association or function) until the sender is available: if there is no data in the channel, the receiver is blocked.

This is illustrated by a simple example:

  

1 Package Main2 3 Import (4     "FMT"5 )6 7Func F1 (inchChanint) {8Fmt. Println (<-inch)9 }Ten  One Func Main () { A      out: = Make (chanint) -      out<-2 -Go F1 ( out) the}

Operating result: Fatal Error:all Goroutines is asleep-deadlock!

This is because there is no receipt for out before line 13th, so for out <-2, it is always blocked, that is, it will always wait.

Swap 13, 14 lines

1 Package Main2 3 Import (4     "FMT"5 )6 7Func F1 (inchChanint) {8Fmt. Println (<-inch)9 }Ten  One Func Main () { A      out: = Make (chanint) -Go F1 ( out) -      out<-2 the}

Run Result: 2

There is a read operation to the pipe before 14 lines, so out <- 2 is legal. As stated earlier, the send operation is blocked before the recipient is ready.

Golang-Channels channel blocking

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.