Metered concurrency: Difference between revisions
→Sync.Cond: simplify, meet documented requirements of sync.Cond
(→Buffered channel: clarity) |
(→Sync.Cond: simplify, meet documented requirements of sync.Cond) |
||
Line 668:
"os"
"sync"
"time"
)
Line 675 ⟶ 674:
type countSem struct {
}
func newCount(n int) *countSem {
return &countSem{
}
func (cs *countSem) count() int {
c := cs.int
cs.L.Unlock()
return c
}
func (cs *countSem) acquire() {
cs.L.Lock()
for cs.int < 0
▲ cs.cond.L.Unlock()
}
cs.L.Unlock()
}
func (cs *countSem) release() {
cs.
cs.L.Unlock()
cs.Broadcast()
}
Line 722 ⟶ 723:
studied.Done()
}</lang>
===Monitor===
Monitor-style solution implements counting semaphore as a monitor goroutine encapsulating the count. It implements semaphore operations with separate Go channels.
|