package conc
import "github.com/sourcegraph/conc"
Index ¶
Examples ¶
Types ¶
type WaitGroup ¶
type WaitGroup struct {
// contains filtered or unexported fields
}
WaitGroup is the primary building block for scoped concurrency. Goroutines can be spawned in the WaitGroup with the Go method, and calling Wait() will ensure that each of those goroutines exits before continuing. Any panics in a child goroutine will be caught and propagated to the caller of Wait().
The zero value of WaitGroup is usable, just like sync.WaitGroup.
Also like sync.WaitGroup, it must not be copied after first use.
Code:
Output:Example¶
{
var count atomic.Int64
var wg WaitGroup
for i := 0; i < 10; i++ {
wg.Go(func() {
count.Add(1)
})
}
wg.Wait()
fmt.Println(count.Load())
// Output:
// 10
}
10
func NewWaitGroup ¶
func NewWaitGroup() *WaitGroup
NewWaitGroup creates a new WaitGroup.
func (*WaitGroup) Go ¶
func (h *WaitGroup) Go(f func())
Go spawns a new goroutine in the WaitGroup.
func (*WaitGroup) Wait ¶
func (h *WaitGroup) Wait()
Wait will block until all goroutines spawned with Go exit and will propagate any panics spawned in a child goroutine.
func (*WaitGroup) WaitAndRecover ¶
WaitAndRecover will block until all goroutines spawned with Go exit and
will return a *panics.Recovered if one of the child goroutines panics.
Code:
Output:Example¶
{
var wg WaitGroup
wg.Go(func() {
panic("super bad thing")
})
recoveredPanic := wg.WaitAndRecover()
fmt.Println(recoveredPanic.Value)
// Output:
// super bad thing
}
super bad thing
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal | |
iter | |
panics | |
pool | |
stream | Package stream provides a concurrent, ordered stream implementation. |
- Version
- v0.3.0 (latest)
- Published
- Feb 25, 2023
- Platform
- linux/amd64
- Imports
- 2 packages
- Last checked
- 4 hours ago –
Tools for package owners.