Search code examples
gochannelmemory-model

How to understand the kth receive on a channel with capacity C happens before the k+Cth send from that channel completes?


It comes from Channel communication.

What I really can't understand is why kth receive happens before the k+Cth send? Why not kth send or k+1 th send?


Solution

  • A capacity of a buffered channel is a number of nonblocked sends to the channel that can happen. When you have used all the capacity (C + k states about it) the next send will be blocked until a receive from the channel and only then it will complete.

    If rephrase the sentence from the link with some numbers like C = 3 and k = 1 (for clarity), it will be like:
    "The fourth send to a channel with a capacity 3 will complete after the first receive from the channel".