Yes, through plain assignment. What problems arise from that?
v := m[key]
m = newMap
mu.Lock()mc := mmu.Unlock()v := mc[key]mu.Lock()m = newMapmu.Unlock()
mu.Lock()v := m[key]mu.Unlock()
> email to golang-nuts+unsubscribe@googlegroups.com.
> 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+unsubscribe@googlegroups.com.
That said, a map is represented by a single machine word (32 or 64 bits). I don't know of any modern architecture where reading a cache aligned word while it is being written will yield anything but the old or new value.
See https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong.
Most Go programmers consider programs with data races to be logically
incorrect / broken.
On Tuesday, September 13, 2016 at 9:48:06 AM UTC+8, Caleb Spare wrote:See https://software.intel.com/en-us/blogs/2013/01/06/benign-data-races-what-could-possibly-go-wrong.I've read this article before and the second reading hasn't done much to improve my impression. The premise in the first example, that multiple threads updating the same memory location in a non-atomic fashion constitutes "innocent" code, is about as strawman as they come. Forget the data race, there's an obvious race condition here! And unless C++ atomic operations are wildly different from those I have experience with (java mainly), the suggested "correct" code fixes only the data race and not the race condition.
And problems emerging from all of these get blamed on the programmer's "racy code" instead of a broken compiler?
I think this will be an interesting read especially if you come from Java.
https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/
--
I think this will be an interesting read especially if you come from Java.
https://shipilev.net/blog/2016/close-encounters-of-jmm-kind/
newMap := make(map[int]int)
newMap[4] = 2
newMap[9] = 3
x.myMap.Store(newMap) //x.myMap is an atomic.Value
table := x.myMap.Load().(map[int]int)
fmt.Println(table[9])
x.myMap = newMap //x.myMap is a map[int]int
x.changed <- nil
<-x.changed
table := x.myMap
fmt.Println(table[9])
package main
import "time"
type T struct{ x int }
var global *T
func f() {
p := new(T)
p.x = 1
global = p // "publish" the new T (racy!)
}
func g() {
p := global
if p != nil {
// println(p.x) // may print "0" due to data race
if p.x == 0 {
panic("ca")
}
}
}
func main() {
for i := 0; i < 10000; i++ {
go func() {
for j := 0; j < 1000; j++ {
f()
}
}()
go func() {
for j := 0; j < 1000; j++ {
g()
}
}()
}
time.Sleep(100 * time.Second)
}