标签:因此 踩坑 统一 idt goroutine 其他 lis const second
func (wg *WaitGroup) Wait()
package main
import "sync"
import "fmt"
import "sync/atomic"
import "time"
const PI = 3.14
var w sync.WaitGroup
var once sync.Once
var doneTaskCount int32
func calCircleCircum(r float64, ret *float64) {
defer w.Done()
*ret = PI * r
atomic.AddInt32(&doneTaskCount, 1)
time.Sleep(time.Millisecond * 2000)
}
func calRectangleCircum(width float64, long float64, ret *float64) {
defer w.Done()
*ret = 2 * (long + width)
atomic.AddInt32(&doneTaskCount, 1)
time.Sleep(time.Millisecond * 1000)
}
func printResults(circleRet *float64, rectangleRet *float64) {
once.Do(func(){
for atomic.LoadInt32(&doneTaskCount) !=2 {
fmt.Println("Wait sub task done.....")
time.Sleep(time.Millisecond * 200)
}
})
fmt.Printf("the cicle circumference is %1.2f\n", *circleRet)
fmt.Printf("the rectangle circumference is %1.2f\n", *rectangleRet)
}
func main() {
circleRet := 0.0
rectangleRet := 0.0
w.Add(2)
go printResults(&circleRet, &rectangleRet)
go calCircleCircum(2.0, &circleRet)
go calRectangleCircum(2.0, 3.0, &rectangleRet)
w.Wait()
}
标签:因此 踩坑 统一 idt goroutine 其他 lis const second
原文地址:https://www.cnblogs.com/linyihai/p/10285437.html