From 2699a5da77458841a26211772bac849e8e7fa6a0 Mon Sep 17 00:00:00 2001 From: sgf Date: Tue, 30 Aug 2022 16:58:05 +0300 Subject: [PATCH] go(conc): More comments. --- go-concurrency-patterns/conc1.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/go-concurrency-patterns/conc1.go b/go-concurrency-patterns/conc1.go index 2738bee..a9442d5 100644 --- a/go-concurrency-patterns/conc1.go +++ b/go-concurrency-patterns/conc1.go @@ -77,10 +77,18 @@ func boring4(msg string) <- chan Message { c := make(chan Message) // Buffering removes a block in main4() loop, when 'true' is send to // 'wait' channel. Thus, next message will first arrive from the guy, who - // slept less. Without buffering, main4() will wait until slower guy - // awakes from 'Sleep' and read 'wait' channel, so the Ann will almost - // always be the first one. + // slept less. + // + // Without buffering in wait channel main4() will wait until _the first_ + // spoke guy awakes from 'Sleep' (or the last - this depends on the order + // of sending to wait channel statements in main4(), see comment there + // too). Thus, even if second guy has woken up from sleep long ago, he's + // forced to wait until the first guy wakes up too. Moreover the second + // guy always receives its "unblock" signal in wait channel _after_ the + // first guy. All these means, that without buffering the order of + // messages in bursts will be the same as in first burst most of the time. waitForIt := make(chan bool, 2) + //waitForIt := make(chan bool) go func() { for i := 0; ; i++ { r := rand.Intn(1e3) @@ -113,10 +121,17 @@ func fanIn4s(input1, input2 <- chan Message) <-chan Message { } func main4() { - c := fanIn4s(boring4("Joe"), boring4("Ann")) + //c := fanIn4s(boring4("Joe"), boring4("Ann")) + c := fanIn4(boring4("Joe"), boring4("Ann")) for i := 0; i < 5; i++ { msg1 := <-c; fmt.Println(msg1.str) msg2 := <-c; fmt.Println(msg2.str) + // Reversing the order in which i "permit speakers to go on", results + // in interleaving order of messages: in the first burst Ann's message + // was last, thus it'll be the first one in the second burst. And in + // the second burst Joey's message was last, thus it'll be the first + // in the third burst. And in the third burst Ann's message was last, + // thus it'll be first in forth burst, etc. msg1.wait <- true msg2.wait <- true } @@ -260,5 +275,5 @@ func main91() { func main() { rand.Seed(time.Now().Unix()) - main9() + main4() } -- 2.20.1