This commit is contained in:
Mark McGranaghan
2012-10-01 08:01:39 -07:00
parent ebec3473f2
commit 9a3987c96a
8 changed files with 139 additions and 315 deletions

View File

@@ -3,33 +3,33 @@ package main
import "fmt"
func main() {
messages := make(chan string)
signals := make(chan bool)
messages := make(chan string)
signals := make(chan bool)
// Non-blocking receive.
select {
case msg := <- messages:
fmt.Println("received message", msg)
default:
fmt.Println("no messages received")
}
// Non-blocking receive.
select {
case msg := <-messages:
fmt.Println("received message", msg)
default:
fmt.Println("no messages received")
}
// Non-blocking send.
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent message", msg)
default:
fmt.Println("no messages sent")
}
// Non-blocking send.
msg := "hi"
select {
case messages <- msg:
fmt.Println("sent message", msg)
default:
fmt.Println("no messages sent")
}
// Non-blocking multi-way select.
select {
case msg := <- messages:
fmt.Println("received message", msg)
case sig := <- signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
// Non-blocking multi-way select.
select {
case msg := <-messages:
fmt.Println("received message", msg)
case sig := <-signals:
fmt.Println("received signal", sig)
default:
fmt.Println("no activity")
}
}

View File

@@ -4,30 +4,30 @@ import "fmt"
import "time"
func main() {
jobs := make(chan bool, 5)
done := make(chan bool)
jobs := make(chan bool, 5)
done := make(chan bool)
go func() {
for {
_, more := <- jobs
if more {
fmt.Println("received job")
} else {
fmt.Println("received all")
done <- true
return
}
}
}()
go func() {
for {
_, more := <-jobs
if more {
fmt.Println("received job")
} else {
fmt.Println("received all")
done <- true
return
}
}
}()
for i := 0; i < 5 ; i++ {
jobs <- false
fmt.Println("sent job")
}
for i := 0; i < 5; i++ {
jobs <- false
fmt.Println("sent job")
}
time.Sleep(100 * time.Millisecond)
close(jobs)
fmt.Println("sent all")
time.Sleep(100 * time.Millisecond)
close(jobs)
fmt.Println("sent all")
<- done
<-done
}

View File

@@ -13,35 +13,35 @@ import "sync/atomic"
// e.g. routing table
type readOp struct {
key int
resp chan int
key int
resp chan int
}
type writeOp struct {
key int
val int
resp chan bool
key int
val int
resp chan bool
}
func randKey() int {
return rand.Intn(10)
return rand.Intn(10)
}
func randVal() int {
return rand.Intn(100)
return rand.Intn(100)
}
func manageState(reads chan *readOp, writes chan *writeOp) {
data := make(map[int]int)
for {
select {
case read := <- reads:
read.resp <- data[read.key]
case write := <- writes:
data[write.key] = write.val
write.resp <- true
}
}
data := make(map[int]int)
for {
select {
case read := <-reads:
read.resp <- data[read.key]
case write := <-writes:
data[write.key] = write.val
write.resp <- true
}
}
}
// Keep track of how many ops we do.
@@ -49,42 +49,42 @@ var opCount int64 = 0
// Generate random reads.
func generateReads(reads chan *readOp) {
for {
key := randKey()
read := &readOp{key: key, resp: make(chan int)}
reads <- read
<- read.resp
atomic.AddInt64(&opCount, 1)
}
for {
key := randKey()
read := &readOp{key: key, resp: make(chan int)}
reads <- read
<-read.resp
atomic.AddInt64(&opCount, 1)
}
}
// Generate random writes.
func generateWrites(writes chan *writeOp) {
for {
key := randKey()
val := randVal()
write := &writeOp{key: key, val: val, resp: make(chan bool)}
writes <- write
<- write.resp
atomic.AddInt64(&opCount, 1)
}
for {
key := randKey()
val := randVal()
write := &writeOp{key: key, val: val, resp: make(chan bool)}
writes <- write
<-write.resp
atomic.AddInt64(&opCount, 1)
}
}
func main() {
reads := make(chan *readOp)
writes := make(chan *writeOp)
reads := make(chan *readOp)
writes := make(chan *writeOp)
go manageState(reads, writes)
go manageState(reads, writes)
for r := 0; r < 100; r++ {
go generateReads(reads)
}
for w := 0; w < 10; w++ {
go generateWrites(writes)
}
for r := 0; r < 100; r++ {
go generateReads(reads)
}
for w := 0; w < 10; w++ {
go generateWrites(writes)
}
atomic.StoreInt64(&opCount, 0)
time.Sleep(time.Second)
finalOpCount := atomic.LoadInt64(&opCount)
fmt.Println(finalOpCount)
atomic.StoreInt64(&opCount, 0)
time.Sleep(time.Second)
finalOpCount := atomic.LoadInt64(&opCount)
fmt.Println(finalOpCount)
}

View File

@@ -10,11 +10,11 @@ import "sync/atomic"
import "runtime"
func randKey() int {
return rand.Intn(10)
return rand.Intn(10)
}
func randVal() int {
return rand.Intn(100)
return rand.Intn(100)
}
// Globally-accessible state.
@@ -27,40 +27,40 @@ var opCount int64 = 0
// Generate random reads.
func generateReads() {
total := 0
for {
key := randKey()
dataMutex.Lock()
total += data[key]
dataMutex.Unlock()
atomic.AddInt64(&opCount, 1)
runtime.Gosched()
}
total := 0
for {
key := randKey()
dataMutex.Lock()
total += data[key]
dataMutex.Unlock()
atomic.AddInt64(&opCount, 1)
runtime.Gosched()
}
}
// Generate random writes.
func generateWrites() {
for {
key := randKey()
val := randVal()
dataMutex.Lock()
data[key] = val
dataMutex.Unlock()
atomic.AddInt64(&opCount, 1)
runtime.Gosched()
}
for {
key := randKey()
val := randVal()
dataMutex.Lock()
data[key] = val
dataMutex.Unlock()
atomic.AddInt64(&opCount, 1)
runtime.Gosched()
}
}
func main() {
for r := 0; r < 100; r++ {
go generateReads()
}
for w := 0; w < 10; w++ {
go generateWrites()
}
for r := 0; r < 100; r++ {
go generateReads()
}
for w := 0; w < 10; w++ {
go generateWrites()
}
atomic.StoreInt64(&opCount, 0)
time.Sleep(time.Second)
finalOpCount := atomic.LoadInt64(&opCount)
fmt.Println(finalOpCount)
atomic.StoreInt64(&opCount, 0)
time.Sleep(time.Second)
finalOpCount := atomic.LoadInt64(&opCount)
fmt.Println(finalOpCount)
}

View File

@@ -4,21 +4,21 @@ import "encoding/base64"
import "fmt"
func main() {
// The data we'll encode/decode.
data := "abc123!?$*&()'-=@~"
fmt.Println(data)
fmt.Println()
// The data we'll encode/decode.
data := "abc123!?$*&()'-=@~"
fmt.Println(data)
fmt.Println()
// Standard base64 encoding/decoding.
stdEnc := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(stdEnc)
stdDec, _ := base64.StdEncoding.DecodeString(stdEnc)
fmt.Println(string(stdDec))
fmt.Println()
// Standard base64 encoding/decoding.
stdEnc := base64.StdEncoding.EncodeToString([]byte(data))
fmt.Println(stdEnc)
stdDec, _ := base64.StdEncoding.DecodeString(stdEnc)
fmt.Println(string(stdDec))
fmt.Println()
// URL base64 encoding/decoding.
urlEnc := base64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(urlEnc)
urlDec, _ := base64.URLEncoding.DecodeString(urlEnc)
fmt.Println(string(urlDec))
// URL base64 encoding/decoding.
urlEnc := base64.URLEncoding.EncodeToString([]byte(data))
fmt.Println(urlEnc)
urlDec, _ := base64.URLEncoding.DecodeString(urlEnc)
fmt.Println(string(urlDec))
}