Add stateful goroutines
This commit is contained in:
78
statefulgoroutines/statefulgoroutines.go
Normal file
78
statefulgoroutines/statefulgoroutines.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package statefulgoroutines
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type readOp struct {
|
||||
key int
|
||||
resp chan int
|
||||
}
|
||||
|
||||
type writeOp struct {
|
||||
key int
|
||||
value int
|
||||
resp chan int
|
||||
}
|
||||
|
||||
func StatefulGoroutines() {
|
||||
var readCounts, writeCounts atomic.Uint64
|
||||
|
||||
reads := make(chan readOp)
|
||||
writes := make(chan writeOp)
|
||||
|
||||
go func() {
|
||||
state := map[int]int{}
|
||||
|
||||
for {
|
||||
select {
|
||||
case r := <-reads:
|
||||
r.resp <- state[r.key]
|
||||
case w := <-writes:
|
||||
state[w.key] = w.value
|
||||
w.resp <- state[w.key]
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Spawn goroutines to do reads
|
||||
for range 100 {
|
||||
go func() {
|
||||
for {
|
||||
r := readOp{
|
||||
key: rand.Intn(5),
|
||||
resp: make(chan int),
|
||||
}
|
||||
reads <- r
|
||||
<-r.resp
|
||||
readCounts.Add(1)
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// Spawn goroutines to do writes
|
||||
for range 10 {
|
||||
go func() {
|
||||
for {
|
||||
w := writeOp{
|
||||
key: rand.Intn(5),
|
||||
value: rand.Intn(100),
|
||||
resp: make(chan int),
|
||||
}
|
||||
writes <- w
|
||||
<-w.resp
|
||||
writeCounts.Add(1)
|
||||
time.Sleep(time.Millisecond)
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
|
||||
fmt.Printf("# of reads => %d\n", readCounts.Load())
|
||||
fmt.Printf("# of writes => %d\n", writeCounts.Load())
|
||||
}
|
||||
Reference in New Issue
Block a user