sorta what I want; doesnt have race conditions

This commit is contained in:
Derek McQuay 2015-11-30 21:27:58 -08:00
parent fe53beecd4
commit 7f6b58b0b1
1 changed files with 71 additions and 0 deletions

71
main.go Normal file
View File

@ -0,0 +1,71 @@
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
var mu sync.Mutex
type queue struct {
size int
load []int
}
func remove(s []int, i int) []int {
copy(s[i:], s[i+1:])
return s[:len(s)-1]
}
func work(q *queue) <-chan string {
ch := make(chan string)
go func() {
for {
mu.Lock()
curLoad := len(q.load)
mu.Unlock()
if curLoad > 0 {
mu.Lock()
curValue := q.load[0]
mu.Unlock()
time.Sleep(time.Millisecond * time.Duration(curValue))
mu.Lock()
q.load = remove(q.load, 0)
mu.Unlock()
ch <- "finished"
}
}
}()
return ch
}
func load(q *queue) {
go func() {
for {
time.Sleep(time.Millisecond * time.Duration(rand.Intn(1000)))
mu.Lock()
q.load = append(q.load, rand.Intn(1000))
mu.Unlock()
}
}()
}
func init() {
rand.Seed(time.Now().UnixNano())
}
func main() {
q := queue{
size: 10,
}
load(&q)
ch := work(&q)
for {
select {
case <-ch:
fmt.Println("finished something")
}
}
}