Golang & Concurrency (50 Qs)•Part 2: Concurrency, Channels & The GMP Scheduler (16 - 32)
Unbuffered vs. Buffered Channels: Internal Data Structure and Blocking Behavior.
senior LevelGolang
In Simple Words:
- Unbuffered Channel (
make(chan int)): Synchronous rendezvous. A send blocks until another goroutine receives; a receive blocks until another goroutine sends. - Buffered Channel (
make(chan int, 10)): Asynchronous buffer. Sends only block when the ring buffer is completely full; receives only block when the buffer is empty.
Internal Structure (hchan in runtime):
- Circular ring buffer array.
- Mutex lock protecting operations.
recvqandsendq: Linked lists of blocked goroutines (sudog) waiting to send or receive.