So the Go 1 memory model really make no guarantees that the following 
program will print 1?

package main

import "fmt"
import "sync/atomic"
import "runtime"

func main() {
    runtime.GOMAXPROCS(1)
    var a, b int32 = 0, 0
    
    go func() {
        a = 1
        atomic.AddInt32(&b, 1)
    }()
    
    for {
        if n := atomic.LoadInt32(&b); n == 1 {
            fmt.Println(a) // always print 1?
            break
        }
        runtime.Gosched()
}

On Sunday, October 22, 2017 at 6:33:42 AM UTC-4, T L wrote:
>
>
>
> On Sunday, October 22, 2017 at 5:55:54 AM UTC-4, Jan Mercl wrote:
>>
>> On Sun, Oct 22, 2017 at 9:19 AM T L <tapi...@gmail.com> wrote:
>>
>> > why?
>>
>> The OP code with line numbes: https://play.golang.org/p/4fmiWqDDc1
>>
>> The main goroutine busy loops at lines 15-20. Because of that the 
>> goroutine at line 10 may never run and thus line 17 may never get executed. 
>> If so, line 18 and thus also lines 27 and above may also never get executed.
>>
>> -- 
>>
>> -j
>>
>
> Aha, yes, if GOMAXPROCS is 1, then the main goroutine will become greedy.
>  
>

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