Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Buffered vs Unbuffered channel in Golang #9

Open
japananh opened this issue Sep 27, 2023 · 0 comments
Open

Buffered vs Unbuffered channel in Golang #9

japananh opened this issue Sep 27, 2023 · 0 comments
Assignees
Labels

Comments

@japananh
Copy link
Owner

japananh commented Sep 27, 2023

Feature Unbuffered Channel Buffered Channel
Definition No storage, transfers data directly between goroutines. Contains a buffer, allowing storage of multiple values.
Capacity 0 (zero) >= 1 (defined at creation)
Creation ch := make(chan int) ch := make(chan int, bufferSize)
Behavior (Send operation) Will block until the sent value is received by another goroutine. Will send immediately if the buffer has space, or else it will block.
Behavior (Receive operation) Will block until a value is sent by another goroutine. Will receive immediately if the buffer has values, or else it will block.
Use Case - Real-time processes where immediate handling is crucial.
- Ensuring step-by-step synchronization between goroutines.
- Situations where senders might momentarily produce data faster than receivers can handle.
- When you want some "elasticity" between producers and consumers of data.
Synchronization Synchronization is direct. When one goroutine sends a value on the channel, it blocks until another goroutine receives that value. This ensures direct hand-off between the sender and the receiver, which can be thought of as a form of strict synchronization. Depends on buffer's state:
For send operation:
- If the buffer is not full, a goroutine can send a value to the channel without blocking. The value goes into the buffer.
- If the buffer is full, the sending goroutine will block until there is space in the buffer (i.e. until some other goroutine receives a value from the channel).
For receive operation:
- If the buffer is not empty, a goroutine can receive a value from the channel without blocking.
- If the buffer is empty, the receiving goroutine will block until there is a value in the buffer (i.e., some other goroutine sends a value to the channel).
@japananh japananh self-assigned this Sep 27, 2023
@japananh japananh changed the title Buffered vs Unbuffered channel in Go Buffered vs Unbuffered channel in Golang Sep 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant