我正在尝试向频道广播一条消息,我只希望它发送 5 条消息。但我总是得到这个错误: 致命错误:所有goroutines都睡着了 - 死锁!
我的代码:
package main
import (
"log"
"sync"
broadcast "github.com/dustin/go-broadcast"
"github.com/pwaller/barrier"
)
//Message boradcasted
type Message struct {
y string
x int
}
var w sync.WaitGroup
var bar barrier.Barrier
func main() {
b := broadcast.NewBroadcaster(100)
w.Add(1)
go workerOne(b)
d := Message{"message :", 0}
go func() {
for i := 0; i < 5; i++ {
d.x = i
log.Printf("Sending %v", d)
b.Submit(d)
}
<-bar.Barrier()
b.Close()
}()
w.Wait()
}
func workerOne(b broadcast.Broadcaster) {
ch := make(chan interface{})
b.Register(ch)
for {
v, ok := <-ch
if ok {
log.Printf("workerOne() reading : %v", v)
} else {
log.Printf("i am here")
close(ch)
b.Unregister(ch)
bar.Fall()
w.Done()
return
}
}
}
我尝试了一切,但它没有抛出条件!在workerOne()函数中确定以关闭通道并结束等待,但仍然有相同的错误
慕容3067478
相关分类