Yp Xie,

The Go scheduler is cooperative: 
https://en.wikipedia.org/wiki/Cooperative_multitasking

You are using one processor: runtime.GOMAXPROCS(1). Force the main 
goroutine to yield: runtime.Gosched().

For example,

https://play.golang.org/p/7HnogkMOOo

0: main
1: main
0: not main
2: main
1: not main
3: main
2: not main
4: main
3: not main
4: not main


Peter

On Sunday, August 20, 2017 at 9:35:50 AM UTC, Yp Xie wrote:
>
> PS: https://play.golang.org/p/ax_bsnEJ1Q
>
> I added more goroutine to see what the result would be:
>
> package main
>
> import (
> "runtime"
> "fmt"
> "sync"
> )
>
> func main() {
> runtime.GOMAXPROCS(1)
> wg := sync.WaitGroup{}
>
> for i := 0; i < 5; i++ {
> wg.Add(1)
> fmt.Printf("%d: main\n", i)
> go func (i int) {
> fmt.Printf("%d: not main\n", i)
> wg.Done()
> } (i)
> }
>
> //time.Sleep(1 * time.Nanosecond)
> wg.Wait()
> }
>
> https://play.golang.org/p/19bgbXJ54-
>
>
>
> The result is like this:
>
> 0: main
> 1: main
> 2: main
> 3: main
> 4: main
> 4: not main
> 0: not main
> 1: not main
> 2: not main
> 3: not main
>
> The fifth goroutine got executed first(except for the main goroutine).
>
> And if I put the main goroutine to sleep for some time by uncomment the 
> sleep line, the result is:
>
>
> 0: main
> 1: main
> 2: main
> 3: main
> 4: main
> 0: not main
> 1: not main
> 2: not main
> 3: not main
> 4: not main
>
>
>  I am really confused with the results.
>
>
> 在 2017年8月20日星期日 UTC+8下午12:57:43,Yp Xie写道:
>>
>> Hi guys,
>>
>> I am learning golang with the fantastic book "Go in Action". And I have 
>> some problems with understanding when to switch a goroutine to be run in 
>> chapter 6.
>>
>>     package main                                                         
>>        
>>   1  
>>   2 import (
>>   3     "fmt"
>>   4     "runtime"
>>   5     "sync"
>>   6 )
>>   7  
>>   8 func main() {
>>   9     runtime.GOMAXPROCS(1)
>>  10  
>>  11     var wg sync.WaitGroup
>>  12     wg.Add(2)
>>  13  
>>         // first goroutine
>>  14     go func() {
>>  15         defer wg.Done()
>>  16         for c := 0; c < 3; c++ {
>>  17             for char := 'a'; char < 'a'+26; char++ {
>>  18                 fmt.Printf("%c", char)
>>  19             }
>>  20         }
>>  21         fmt.Println("")
>>  22     }()
>>  23     
>>         // second goroutine
>>  24     go func() {
>>  25         defer wg.Done()
>>  26         for c := 0; c < 3; c++ {
>>  27             for char := 'A'; char < 'A'+26; char++ {
>>  28                 fmt.Printf("%c", char)
>>  29             }
>>  30         }
>>  31         fmt.Println("")
>>  32     }()
>>  33  
>>  34     wg.Wait()
>>      }
>>
>>
>> Why the result is printing uppercase letters first?
>>
>> I mean the two goroutines are passed in a waiting queue in order of the 
>> time they were generated, and they were meant to be executed in the same 
>> order.
>>
>> So why the second goroutine got executed first? 
>>
>> Or is the fmt.Println method is IO-related?
>>
>> Please help me understand this situation.
>>
>> Thanks!
>>
>>
>>

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