mirror of
https://github.com/golang/go
synced 2024-09-24 23:20:12 -06:00
sync: add Once example
R=golang-dev, rsc, bradfitz CC=golang-dev https://golang.org/cl/5715046
This commit is contained in:
parent
986df83e0d
commit
2295554db6
@ -5,6 +5,7 @@
|
||||
package sync_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sync"
|
||||
)
|
||||
@ -32,3 +33,22 @@ func ExampleWaitGroup() {
|
||||
// Wait for all HTTP fetches to complete.
|
||||
wg.Wait()
|
||||
}
|
||||
|
||||
func ExampleOnce() {
|
||||
var once sync.Once
|
||||
onceBody := func() {
|
||||
fmt.Printf("Only once\n")
|
||||
}
|
||||
done := make(chan bool)
|
||||
for i := 0; i < 10; i++ {
|
||||
go func() {
|
||||
once.Do(onceBody)
|
||||
done <- true
|
||||
}()
|
||||
}
|
||||
for i := 0; i < 10; i++ {
|
||||
<-done
|
||||
}
|
||||
// Output:
|
||||
// Only once
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user