As Ian said, your question is not completely clear. I think you are Saying 
that "I got scheduled!" prints 3 times, then the program appears to "hangs" 
on the runtime.GC() call.

To start with, it is usually helpful to create a *minimal *program when 
dealing with questions like this. Your program can be reduced to 
https://play.golang.org/p/-9ekKOxyVoc:
package main

import (
    "fmt"
    "runtime"
    "time"
)

func deadloop() {
    for {
        //runtime.Gosched()
    }
}
func main() {

    go deadloop()

    i := 3
    for {
        time.Sleep(time.Second * 1)
        i--
        fmt.Println("I got scheduled!", i)
        if i == 0 {
            runtime.GC() //will set gcwaiting=1, causes other goroutines 
not to be scheduled
        }
    }
}
This prints:
I got scheduled! 2
I got scheduled! 1
I got scheduled! 0
Then hangs. 

I believe that what you are seeing is a result of the GC needing to briefly 
pause all goroutines. Go uses something like cooperative multithreading for 
goroutines. Your deadloop() function can not be preempted under the current 
implentation of the go compiler. So the call to runtime.GC() hangs waiting 
to pause all goroutines. If you un comment the runtime.Gosched() call on 
line 11, then your program will work as expected. 

The details of premtion are implementation dependant, and work is being 
done to improve the situation. There is a lot of information available, but 
if you have a long running loop that makes no function calls, then it is a 
good idea to call runtime.Gosched() every so often.

It is also important to note that the behavior of runtime.GC() is not the 
exactly same as the normal backgound GC that gets run by the runtime. In 
particular runtime.GC() will block until GC is completed, whereas the noral 
gc does most of its work while allowing the program to continue running. 

Hope that helps.


On Wednesday, April 10, 2019 at 7:51:52 AM UTC-4, mount...@gmail.com wrote:
>
> Hi,
>
> My  macos has 4 cores, but i start 3 goroutines, 2 sub goroutines, 1 main 
> goroutine. 
> After main goroutine  exec 3 times  output, triggering gc causes 
> gcwaiting=1.
> Finally all goroutine blocking
>
> package main
>
> import (
> "fmt"
> "log"
> "net/http"
> _ "net/http/pprof"
> "runtime"
>
> // "runtime"
> "time"
> )
>
> func deadloop() {
> for {
> }
> }
> func main() {
> go func() {
> log.Println(http.ListenAndServe("localhost:6060", nil))
> }()
>
> go deadloop()
>
> i := 3
> for {
> time.Sleep(time.Second * 1)
> i--
> fmt.Println("I got scheduled!")
> if i == 0 {
> runtime.GC() //will set gcwaiting=1, causes other goroutines not to be 
> scheduled
> }
> }
> }
>

-- 
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.

Reply via email to