Got it. Thank you. 在 2017年4月22日星期六 UTC+8上午9:52:45,Matt Harden写道: > > There is no guarantee that any of your goroutines will execute even > Lock(), much less Wait(), before the main goroutine executes Broadcast(). > > On Thu, Apr 20, 2017 at 8:39 PM Allan <alla...@gmail.com <javascript:>> > wrote: > >> I run a demo program to learn sync.NewCond: >> >> package main >> >> import ( >> "fmt" >> "sync" >> "time" >> ) >> >> var locker = new(sync.Mutex) >> var cond = sync.NewCond(locker) >> >> func test(x int) { >> >> cond.L.Lock() >> cond.Wait() >> fmt.Println(x) >> time.Sleep(time.Second * 1) >> cond.L.Unlock() >> } >> func main() { >> for i := 0; i < 10; i++ { >> go test(i) >> } >> fmt.Println("start all") >> cond.Broadcast() >> time.Sleep(time.Second * 60) >> } >> >> >> When I run this program on Mac(Sierra 10.12.4),it will print 0~9 >> randomly. But When I run it on Ubuntu 16.04, its result is not unique, >> sometime just print "start all", sometimes print some numbers but not all. >> I debug it with dlv, and the stack is as below: >> >> (dlv) bt >> 0 0x0000000000453693 in runtime.futex >> at /usr/local/go/src/runtime/sys_linux_amd64.s:388 >> 1 0x00000000004238de in runtime.futexsleep >> at /usr/local/go/src/runtime/os_linux.go:62 >> 2 0x000000000040c370 in runtime.notetsleep_internal >> at /usr/local/go/src/runtime/lock_futex.go:174 >> 3 0x000000000040c4e2 in runtime.notetsleepg >> at /usr/local/go/src/runtime/lock_futex.go:206 >> 4 0x000000000043fff8 in runtime.timerproc >> at /usr/local/go/src/runtime/time.go:209 >> 5 0x00000000004525f0 in runtime.goexit >> at /usr/local/go/src/runtime/asm_amd64.s:2086 >> (dlv) goroutine >> Thread 9327 at /usr/local/go/src/runtime/sys_linux_amd64.s:388 >> Goroutine 15: >> Runtime: /usr/local/go/src/runtime/lock_futex.go:206 runtime.notetsleepg >> (0x40c4e2) >> User: /usr/local/go/src/runtime/lock_futex.go:206 runtime.notetsleepg >> (0x40c4e2) >> Go: /usr/local/go/src/runtime/time.go:97 runtime.addtimerLocked (0x43fc2e) >> (dlv) goroutines >> [15 goroutines] >> Goroutine 1 - User: /usr/local/go/src/runtime/time.go:59 time.Sleep >> (0x43fa55) >> Goroutine 2 - User: /usr/local/go/src/runtime/proc.go:260 >> runtime.gopark (0x4283fa) >> Goroutine 3 - User: /usr/local/go/src/runtime/proc.go:260 >> runtime.gopark (0x4283fa) >> Goroutine 4 - User: /usr/local/go/src/runtime/proc.go:260 >> runtime.gopark (0x4283fa) >> Goroutine 5 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 6 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 7 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 8 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 9 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 10 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 11 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 12 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 13 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> Goroutine 14 - User: /usr/local/go/src/runtime/sema.go:267 >> sync.runtime_notifyListWait (0x436341) >> * Goroutine 15 - User: /usr/local/go/src/runtime/lock_futex.go:206 >> runtime.notetsleepg (0x40c4e2) >> >> >> I'm not familiar to the raw code,can someone give me some help. Thx. >> >> BTW: My golang version is go version go1.7.4 linux/amd64. >> >> -- >> You received this message because you are subscribed to the Google Groups >> "golang-nuts" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to golang-nuts...@googlegroups.com <javascript:>. >> For more options, visit https://groups.google.com/d/optout. >> >
-- You received this message because you are subscribed to the Google Groups "golang-nuts" group. To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.