Photo taken on September 25, 2021 shows the Xixi Wetland in Hangzhou.

The last article focused on the channel runtime represented by hchan and briefly explained the meaning of the hchan fields.

We mentioned that the operation on channel is essentially the operation on the field in hchan. Because the mutex is used during the operation, the concurrency security of the channel is guaranteed.

This article mainly uses some real life examples to illustrate some principles of channel, of course, will not involve too much source code.

There is no buffer

As we all know, channels are divided into unbuffered and buffered. What’s the biggest difference between the two?

Let’s use a real life delivery example.

The above scene is the Courier waiting for the small warehouse, of course, the small warehouse may also be waiting for the Courier.

If there is no express box, the Courier in the process of delivering the express, if there is no one at home, he has to wait there, waiting for someone to sign for the express, he will finish the delivery.

The customer can’t leave home before the Courier arrives, otherwise no one will collect the Courier, so he has to wait for the Courier to come to the door, sign to receive the Courier, he can count the delivery is over.

Of course, the customer has more than one Courier, and if Courier A is waiting for another Courier B to deliver to him. Courier B not only had to wait, he had to wait in line. When the customer gets home, he must first sign for A’s express delivery, and then sign for B’s express delivery.

Corresponds to an unbuffered channel,

When sending data, if there is no corresponding receiverready, then the sender enters the waiting send queue, waiting for a corresponding receiver to wake it up.

When receiving data, if there is no corresponding senderready, then the receiver enters the waiting receive queue, waiting for a corresponding sender to wake it up.

Remember in the last article we introduced the structure of HCHAN.

Recvq indicates the queue waiting to receive messages, and sendq indicates the queue waiting to send messages.

Let’s look at waitq.

In essence, WaitQ is a linked list, or rather a bidirectional circular list. Waitq records the beginning and end of the linked list, and sudog records the last (prev) and next (next) of the current wait.

It’s like Coop signing for A’s delivery and saying, who’s next?

A will say: My next one is B.

B will say: It’s me. I remember my last one was A, and I don’t have A next one at the moment, so I’m the last one.

The buffer

Having looked at unbuffered queues, let’s look at buffered queues. Again, using the story above,

As long as there is a spare cabinet in the express cabinet, the Courier can put the express directly into the cabinet and let the customer go to the cabinet to get it. If there is no free cabinet for delivery, I have to wait until someone tells me there is a free cabinet, and then I put the delivery into the free cabinet.

Corresponding to the buffer channel, the express container above is the buffer that stores data in the buffer channel.

For the sender: as long as the buffer is full, the sender can continue to send data to the buffer. Once the buffer is full, the sender can only enter the waiting send queue, wait for a corresponding receiver to wake it up, and then put the data into the location where it was just fetched.

For the receiver: as long as the buffer is not empty, the receiver can continue to receive data. Once the buffer is empty, the receiver can only go into a waiting receive queue and wait for a corresponding sender to wake it up.

Are there any questions on it? Also is there.

When we pick up the express, will you pick up the express according to the order in which the express is put into the express cabinet? No way.

In a channel, however, messages are guaranteed a first-in, first-out (FIFO) relationship. As for guarantees, we’ll leave that to the code details at the end of this article.

conclusion

This article mainly introduces the principle of channel operation through an example of express. In the next article, we’ll look at the details of channel’s logic for the above processing.

This article uses the article synchronization assistant to synchronize