Re: [go-nuts] How is a for loop preempted in Go?

2019-02-23 Thread Ian Denhardt
Quoting David Finkel (2019-02-23 16:09:18) >Not only is fmt.Printf a function call, but it makes a blocking >syscall >(write specifically), >which gives the go runtime license to spin up a new OS thread and >immediately schedule the other goroutine. >runtime.GOMAXPROCS(1)

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-23 Thread David Finkel
[The other responses have been excellent, but I wanted to point one thing out.] On Fri, Feb 22, 2019 at 3:17 AM Jingguo Yao wrote: > Consider the following code which launches two goroutines. Each > goroutine uses a for loop to print messages. > > package main > > import ( > "fmt" >

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-23 Thread Jingguo Yao
Ian: Thanks for your explanation. On Saturday, February 23, 2019 at 1:13:04 AM UTC+8, Ian Denhardt wrote: > > In the new example, goroutine 2 should get pre-empted at time.Sleep. > goroutine 1 may hog a CPU however. There's an open issue about this, > which is being worked on. > >

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-22 Thread Ian Denhardt
In the new example, goroutine 2 should get pre-empted at time.Sleep. goroutine 1 may hog a CPU however. There's an open issue about this, which is being worked on. https://github.com/golang/go/issues/10958 I think it's always been the intent that from the programmer's perspective goroutines

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-22 Thread Jingguo Yao
Yes, my fault. Thanks for pointing it out. What happens if I remove the fmt.Printf function calls to have the following code: package main import ( "fmt" "runtime" "time" ) func main() { count := runtime.GOMAXPROCS(1) fmt.Printf("GOMAXPROCS: %d\n", count) // goroutine 1 go func() { for { }

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-22 Thread Jan Mercl
On Fri, Feb 22, 2019 at 9:17 AM Jingguo Yao wrote: > Without preemption, the M will run goroutine 1 or goroutine 2 continuously. Not correct, there's also cooperative scheduling/yielding. > The messages sent to the terminal should be all 1s or all 2s. Nothing in the language specs mandates

Re: [go-nuts] How is a for loop preempted in Go?

2019-02-22 Thread Ian Denhardt
Quoting Jingguo Yao (2019-02-22 03:17:33) >Since both goroutines do not make any function calls fmt.Printf is a function. -- 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

[go-nuts] How is a for loop preempted in Go?

2019-02-22 Thread Jingguo Yao
Consider the following code which launches two goroutines. Each goroutine uses a for loop to print messages. package main import ( "fmt" "runtime" "time" ) func main() { count := runtime.GOMAXPROCS(1) fmt.Printf("GOMAXPROCS: %d\n", count) // goroutine 1 go func() { for