mirror of
https://github.com/golang/go
synced 2024-11-20 06:24:40 -07:00
add note about once and closures
R=r DELTA=13 (13 added, 0 deleted, 0 changed) OCL=31535 CL=31549
This commit is contained in:
parent
32cd887574
commit
8afc6008a8
@ -23,6 +23,19 @@ var joblock sync.Mutex;
|
|||||||
// If multiple processes call Do(f) simultaneously
|
// If multiple processes call Do(f) simultaneously
|
||||||
// with the same f argument, only one will call f, and the
|
// with the same f argument, only one will call f, and the
|
||||||
// others will block until f finishes running.
|
// others will block until f finishes running.
|
||||||
|
//
|
||||||
|
// Since a func() expression typically evaluates to a differerent
|
||||||
|
// function value each time it is evaluated, it is incorrect to
|
||||||
|
// pass such values to Do. For example,
|
||||||
|
// func f(x int) {
|
||||||
|
// Do(func() { fmt.Println(x) })
|
||||||
|
// }
|
||||||
|
// behaves the same as
|
||||||
|
// func f(x int) {
|
||||||
|
// fmt.Println(x)
|
||||||
|
// }
|
||||||
|
// because the func() expression in the first creates a new
|
||||||
|
// func each time f runs, and each of those funcs is run once.
|
||||||
func Do(f func()) {
|
func Do(f func()) {
|
||||||
joblock.Lock();
|
joblock.Lock();
|
||||||
j, present := jobs[f];
|
j, present := jobs[f];
|
||||||
|
Loading…
Reference in New Issue
Block a user