R
R
raiboon2015-05-29 15:20:49
go
raiboon, 2015-05-29 15:20:49

How are buffered channels different from unbuffered ones?

I can't figure out what's the difference? Can you explain with examples?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SilentFl, 2015-05-29
@SilentFl

think of channels as queues. unbuffered - with length 1, buffered - with length n (ch := make(chan struct{}, n). Writing to the channel can be done if there are still places in the channel, when the channel is full, the recording is locked (i.e. code " gets up" on this attempt and waits for a seat to become available). according to this:

ch1 := make(chan struct{})
ch1 <- struct{}{} //ок, ушло в канал
ch1 <- struct{}{} //висим и ждем когда кто-нить прочитает из канала

ch2 := make(chan struct{},3)
ch2 <- struct{}{} //ок, ушло в канал
ch2 <- struct{}{} //также ушло
ch2 <- struct{}{} //и еще ушло
ch2 <- struct{}{} //а вот тут лок. висим и ждем когда кто-то прочитает из канала

M
Mikhail Potanin, 2015-06-30
@potan

In an unbuffered channel, the sender can determine whether a message has been received. Sometimes this is important, for example, for synchronization instead of semaphores, although it does not guarantee that the recipient will be able to process it, and not immediately fall.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question