infra
/
goutils
Archived
1
0
Fork 0
This repository has been archived on 2022-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
goutils/main.go

60 lines
990 B
Go

package main
import (
"fmt"
mys "amuz.es/src/infra/goutils/sync"
"sync"
"time"
"math/rand"
)
type philosopher struct {
eating bool
left *philosopher
right *philosopher
//table
}
var current []int
func anotherRoutine(cond *sync.Cond, j int) {
vb := time.Duration(rand.Intn(10)+1000) * time.Millisecond
time.Sleep(vb)
cond.L.Lock()
defer cond.L.Unlock()
cond.Wait()
current = append(current, j)
}
func monitor(cond *sync.Cond, i int, doneChan chan<- struct{}) {
defer close(doneChan)
j := 0
for {
cond.L.Lock()
if j == len(current) {
cond.Signal()
} else {
j++
//fmt.Printf("%d\n", current[j-1:j])
fmt.Printf("%d\n", j)
//fmt.Println(current)
}
cond.L.Unlock()
if i == j {
return
}
}
}
func main() {
i := 10000
doneChan := make(chan struct{})
cond := sync.NewCond(&mys.ReentrantMutex{})
go monitor(cond, i, doneChan)
for j := 0; j < i; j++ {
go anotherRoutine(cond, j)
}
<-doneChan
fmt.Printf("done %d\n", len(current))
}