concurrency patterns: round-trip

134 views
Skip to first unread message

matt

unread,
Feb 27, 2013, 11:55:08 AM2/27/13
to golan...@googlegroups.com
Rob Pike's example in slide http://talks.golang.org/2012/concurrency.slide#38 mentions a round-trip pattern where you block on a read to a channel you have just written into. What are the rules associated with this pattern?

In particular I tried this out and I'm coming unstuck as the go routine that reads from the channel, writes back into it, and then reads its own value out of the channel in the next select body.

I'll try and distil a small example if it helps...

Kamil Kisiel

unread,
Feb 27, 2013, 12:01:17 PM2/27/13
to golan...@googlegroups.com
Are you using a buffered channel? If you have an unbuffered channel as in the example then the send should block until another goroutine performs a receive.

Nate Finch

unread,
Feb 27, 2013, 12:24:20 PM2/27/13
to golan...@googlegroups.com
Writing to the channel will block until someone reads the value, unless the channel is buffered (rob was using unbuffered channels).


the key part is this:

// this send on the channel blocks until the worker goroutine reads from it
quit <- "Say goodbye, Joe!"

// the worker goroutine must have already read the quit you sent at this point
// so now we're waiting for the quit message sent *from* the worker goroutine
fmt.Println(<-quit)

matt

unread,
Feb 27, 2013, 5:39:55 PM2/27/13
to golan...@googlegroups.com
Thanks that's the bit I'd overlooked, its not buffered. Makes sense.
Reply all
Reply to author
Forward
0 new messages