Yes, I just want to let the bar func goroutine run first or replace runtime.Gosched() with time.Sleep, But I still doubt why add1 in bar func don't happen preempt schedule ?
在 2017年9月4日星期一 UTC+8下午3:25:04,John Souvestre写道: > > I think that you put the call to runtime.Gosched() in the wrong place. > You want to place it in the routine which needs to be pre-empted. In your > example, in add1() inside the “for i” loop or in bar() in the “for” loop. > > > > John > > John Souvestre - New Orleans LA > > > > *From:* golan...@googlegroups.com <javascript:> [mailto: > golan...@googlegroups.com <javascript:>] *On Behalf Of *??? > *Sent:* 2017 September 04, Mon 02:10 > *To:* golang-nuts > *Subject:* Re: [go-nuts] golang gc stop the world how to stop cpu busy > goroutine? > > > > package main > > > > import "runtime" > > > > // GOMAXPROCS=1 > > var sum1, summain int64 > > > > func add1() { > > for i := 0; i < 10000; i++ { > > for j := 0; j < 10000; j++ { > > sum1++ > > } > > } > > } > > > > func bar() { > > for { > > add1() > > } > > } > > > > func main() { > > go bar() > > runtime.Gosched() > > println("debug>>>") > > } > > > above is my example code,when I set GOMAXPROCS=1 and build with -gcflags > "-N -l" the println("debug>>>") don't execute > > I just doubt the go runtime support peempt schedule in call function, it > don't work ?? > > > > 在 2017年9月4日星期一 UTC+8下午2:20:37,John Souvestre写道: > > Although a goroutine isn’t pre-emptible by the Go scheduler, there is an > opportunity whenever it does something which blocks or when it calls a > non-inlined function. So generally the GC’s STW can take place pretty > quickly. But if you are doing something with is totally compute bound then > it can be a problem. An easy solution is to insert a call to > runtime.Gosched() every so often. > > > > I believe that there is an ongoing discussion about a way for Go to plan > ahead and be able to handle even these cases, but it’s something for the > future. > > > > John > > John Souvestre - New Orleans LA > > > > *From:* golan...@googlegroups.com [mailto:golan...@googlegroups.com] *On > Behalf Of *??? > *Sent:* 2017 September 04, Mon 00:47 > *To:* golang-nuts > *Subject:* [go-nuts] golang gc stop the world how to stop cpu busy > goroutine? > > > > when have one or two cpu busy goroutine in my server (example for loop > empty), the gc stop the world how to stop the goroutine ? > > -- > 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. > 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...@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.