Use the same method for both Load and Set, also your example will never complete as your for loops will run forever.

On 05/07/2019 15:53, Cholerae Hu wrote:
|
package main

import (
"sync/atomic"
"sync"
)

func main() {
var n int32
var m sync.Mutex
var wg sync.WaitGroup
wg.Add(2)
go func() {
for {
atomic.LoadInt32(&n)
}
wg.Done()
}()
go func() {
for {
m.Lock()
n = 1
m.Unlock()
}
wg.Done()
}()
wg.Wait()
}

|
Does it safe to use atomic read an int and write an int non-atomically but in lock concurrently? Race detector will report it data race.
--
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 <mailto:golang-nuts+unsubscr...@googlegroups.com>. To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/825f9031-74b9-41b6-87af-dccfa7d98d04%40googlegroups.com <https://groups.google.com/d/msgid/golang-nuts/825f9031-74b9-41b6-87af-dccfa7d98d04%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/3fdeff39-758e-9a00-ba51-f79c4d20bde8%40multiplay.co.uk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to