Documentation ¶
Overview ¶
Package congo provides concurrency primitives for Go.
Index ¶
- Variables
- type CountDownLatch
- func (latch *CountDownLatch) Complete() error
- func (latch *CountDownLatch) Count() uint
- func (latch *CountDownLatch) CountDown() error
- func (latch *CountDownLatch) Wait()
- func (latch *CountDownLatch) WaitTimeout(timeout time.Duration) bool
- func (latch *CountDownLatch) WeightedCountDown(weight uint) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCountDownLatchCompleted is returned when CountDown or WeightedCountDown is called after the count down is already complete ErrCountDownLatchCompleted = errors.New("Latch count down already complete") )
These are errors related to CountDownLatch.
Functions ¶
This section is empty.
Types ¶
type CountDownLatch ¶
type CountDownLatch struct {
// contains filtered or unexported fields
}
A CountDownLatch is used to signal the completion of a specified number of events.
In typical usage, a main goroutine creates the latch with a provided count and passes the latch to a number of goroutines. The goroutines invoke CountDown (or WeightedCountDown) on the latch to signal completion and reduce the remaining count on the latch. The main goroutine invokes Wait or WaitTimeout on the latch to wait for all events to complete.
CountDownLatch is similar to sync.WaitGroup but differs in a few ways:
- It has the ability to wait with a timeout (WaitTimeout) for latch to complete.
- It has the ability to get the current count using the Count method.
- It has ability to do a WeightedCountDown, i.e. reduce the remaining count by more than 1.
- It has the ability to Complete the count down immediately, unblocking any goroutines waiting on Wait
- The count is set one time at latch creation instead of through a separate Add call.
Example ¶
latch := NewCountDownLatch(3) for i := 0; i < 3; i++ { go func() { defer latch.CountDown() // count down after work is complete // do work // ... fmt.Println("Counting down") }() } if latch.WaitTimeout(5 * time.Second) { fmt.Println("Count down complete") } else { fmt.Println("Count down not complete") }
Output: Counting down Counting down Counting down Count down complete
func NewCountDownLatch ¶
func NewCountDownLatch(count uint) *CountDownLatch
NewCountDownLatch creates a CountDownLatch with the provided count. If the count is 0, the latch will be set to immediately signal count down completion for any goroutines that subsequently call Wait or WaitTimeout.
func (*CountDownLatch) Complete ¶
func (latch *CountDownLatch) Complete() error
Complete reduces the count on the CountDownLatch by its current remaining count. It is equivalent to calling WeightedCountDown with the remaining count.
func (*CountDownLatch) Count ¶
func (latch *CountDownLatch) Count() uint
Count returns the remaining count.
func (*CountDownLatch) CountDown ¶
func (latch *CountDownLatch) CountDown() error
CountDown is equivalent to WeightedCountDown with a weight of 1.
func (*CountDownLatch) Wait ¶
func (latch *CountDownLatch) Wait()
Wait waits indefinitely until the count down is completed. Wait returns immediately if the count down has already been completed.
func (*CountDownLatch) WaitTimeout ¶
func (latch *CountDownLatch) WaitTimeout(timeout time.Duration) bool
WaitTimeout waits until a given timeout for the count down to complete. If the count down is completed before the timeout, WaitTimeout returns true. Otherwise it returns false. WaitTimeout returns immediatley if the count down has already been completed.
func (*CountDownLatch) WeightedCountDown ¶
func (latch *CountDownLatch) WeightedCountDown(weight uint) error
WeightedCountDown reduces the count on the CountDownLatch by the given weight. If given weight exceeds the current latch's count, the latch's count is set to 0. If the count hits 0, WeightedCountDown signals latch count down completion, waking up any goroutines waiting with Wait or WaitTimeout. An error, ErrCountDownLatchCompleted, is returned if count down has already been completed.