Thanks all for the explanations.

I still have one question, which has been mentioned above.

> For any sync.Mutex or sync.RWMutex variable l and *n* < *m*, call *n* of 
l.Unlock() happens before call *m* of l.Lock() returns. 

What are the "n" and "m" here?

On Saturday, October 21, 2017 at 5:01:08 PM UTC-4, T L wrote:
>
> package main
>
> import "fmt"
> import "sync"
> import "sync/atomic"
>
> func main() {
>     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
>         }
>     }
>     
>     //======================
>     
>     var x, y int32 = 0, 0
>     var m sync.Mutex
>     
>     go func() {
>         x = 1
>         m.Lock()
>         y++
>         m.Unlock()
>     }()
>     
>     for {
>         m.Lock()
>         n := y
>         m.Unlock()
>         if n == 1 {
>             fmt.Println(x) // always print 1?
>             break
>         }
>     }
> }
>

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