Search code examples
gosynchronizationchannel

go buffered channel synchronization


goroutine x,y
channel a, 5 capacity of buffer Here is the thing: if there is an occasion that when a is full and x is trying to put a new item into a, at x gets blocked. And when the time that y comes to get an item from a. Is this the occasion that x input and y get can be seen as the synchronization action?,Can this be seen as an unbuffered channel interaction?(they act simultaneously but just put/get different items) Just like an unbuffered channel when x input and y get.

I am sorry that i have no idea how to verify and do some test with my question. So i am really looking forward for some insights and interpretation.


Solution

  • Yes, it's a synchronization point.

    The Go Memory Model says: “The kth receive on a channel with capacity C is synchronized before the completion of the k+Cth send from that channel completes.”

    So in your example capacity C is 5 and k is 1, and the 1st receive (in goroutine y) is synchronized before the completion of the 6th send (from coroutine x) completes.

    Try it on the Go Playground. Note that technically only the send/receive is synchronized and the Println statements could show up in a different order - this is only for demonstration purposes.