You can not select more than 25 topics
Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
- package sync2
-
- import "sync"
-
- type CounterCond struct {
- count int
- cond *sync.Cond
- }
-
- func NewCounterCond(initCount int) *CounterCond {
- return &CounterCond{
- count: initCount,
- cond: sync.NewCond(&sync.Mutex{}),
- }
- }
-
- func (c *CounterCond) Wait() bool {
- c.cond.L.Lock()
- defer c.cond.L.Unlock()
-
- for c.count == 0 {
- c.cond.Wait()
-
- if c.count == 0 {
- return false
- }
- }
-
- c.count--
-
- return true
- }
-
- func (c *CounterCond) Release() {
- c.cond.L.Lock()
- c.count++
- c.cond.L.Unlock()
- c.cond.Signal()
- }
-
- // WakeupAll 不改变计数状态,唤醒所有等待线程
- func (c *CounterCond) WakeupAll() {
- c.cond.Broadcast()
- }
|