Merge branch 'stateful-goroutines-internal-state'

This commit is contained in:
Mark McGranaghan 2013-10-20 14:31:37 -07:00
commit b58964e831

View File

@ -36,12 +36,7 @@ type writeOp struct {
func main() { func main() {
// The `state` will be a map as in the previous // As before we'll count how many operations we perform.
// example.
var state = make(map[int]int)
// Also as before we'll count how many operations we
// perform.
var ops int64 = 0 var ops int64 = 0
// The `reads` and `writes` channels will be used by // The `reads` and `writes` channels will be used by
@ -50,14 +45,18 @@ func main() {
reads := make(chan *readOp) reads := make(chan *readOp)
writes := make(chan *writeOp) writes := make(chan *writeOp)
// Here is the goroutine that owns the `state`. This // Here is the goroutine that owns the `state`, which
// goroutine repeatedly selects on the `reads` and // is a map as in the previous example but now private
// `writes` channels, responding to requests as they // to the stateful goroutine. This goroutine repeatedly
// arrive. A response is executed by first performing // selects on the `reads` and `writes` channels,
// the requested operation and then sending a value // responding to requests as they arrive. A response
// on the response channel `resp` to indicate success // is executed by first performing the requested
// (and the desired value in the case of `reads`). // operation and then sending a value on the response
// channel `resp` to indicate success (and the desired
// value in the case of `reads`).
go func() { go func() {
var state = make(map[int]int)
for { for {
select { select {
case read := <-reads: case read := <-reads: