Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is go Channel?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "what is go Channel". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is go Channel".

Channel

Channel is the communication mode between the core data structure of Go and Goroutine, and Channel is an important part of the high concurrency variable programming model of Go. The chan keyword is usually used in combination with range or select.

Design principle:

In java or some other languages, the most criticized way to write multithreaded code is to communicate through shared memory. Usually for this type of code, we recommend sharing memory through communication, for example, in java we can allow threads to block to acquire mutexes. However, in the language design of Go, it deviates from the appeal model.

Goroutine and Channel correspond to entities in CSP and media for transmitting information, respectively, and data is transmitted between Goroutine through Channel.

In Go, although mutexes are used for communication like java or other languages, Go provides another concurrency model, Communication Sequential processes (Communicating sequential processes,CSP). You can use Channel to transfer data between Goroutine.

Features:

Multiple chan run independently and are not related to each other. Goroutine that reads data from Channel first receives data; Goroutine that sends data to Channel first gets the right to send data first; in some sense, chan is actually a locked queue for synchronization and communication.

This is the data structure of chan.

Type hchan struct {

Qcount uint / / total data in the queue

Dataqsiz uint / / size of the circular queue

Buf unsafe.Pointer / / points to an array of dataqsiz elements

Elemsize uint16

Closed uint32

Elemtype * _ type / / element type

Sendx uint / / send index

Recvx uint / / receive index

Recvq waitq / / list of recv waiters

Sendq waitq / / list of send waiters

/ / lock protects all fields in hchan, as well as several

/ / fields in sudogs blocked on this channel.

/ /

/ / Do not change another G's status while holding this lock

/ / (in particular, do not ready a G), as this can deadlock

/ / with stack shrinking.

Lock mutex

}

The number of elements in qcount-Channel; the length of the circular queue in dataqsiz-Channel; the buffer data pointer of buf-Channel; the location to which the send operation of sendx-Channel is processed; the location where the receive operation of recvx-Channel is processed; the basic operation of chan

Create a new chan

/ / unbuffered pipeline

C1: = make (chan int)

/ / buffer pipeline

C2: = make (chan int, 10) buffer pipe unbuffered pipe: will block the buffer pipe: will not block until the pipe reaches the buffer size

Send data

C: = make (chan int, 10)

C

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report