In my write goroutine I don't modify the map key (data structure) but set the variable point to another map address ,this is a pointer assignment and is atomic
在 2016年11月4日星期五 UTC+8下午1:22:20,Ian Lance Taylor写道: > > On Thu, Nov 3, 2016 at 10:19 PM, 刘桂祥 <liuguix...@gmail.com <javascript:>> > wrote: > > can you explain why whis ? > > A map is basically a pointer to a complex data structure. Setting a > value in a map changes that data structure. If one goroutine is > reading from the data structure while a different goroutine is writing > to the data structure, the results are completely unpredictable. In > the worst case they could even cause the program to crash. > > Ian > > > > 在 2016年11月4日星期五 UTC+8下午1:16:39,Ian Lance Taylor写道: > >> > >> On Thu, Nov 3, 2016 at 8:37 PM, 刘桂祥 <liuguix...@gmail.com> wrote: > >> > // example.go > >> > > >> > package main > >> > > >> > var gMap = make(map[int]int) > >> > > >> > func w() { > >> > temp := make(map[int]int) > >> > temp[1] = 100 > >> > temp[2] = 200 > >> > gMap = temp // Does the compiler or cpu will reorder > temp[1]=100, > >> > temp[2]=200, gMap=temp ?? > >> > } > >> > > >> > func r() { > >> > local := gMap > >> > println(local[1], local[2]) > >> > } > >> > > >> > func main() { > >> > > >> > go w() > >> > go r() > >> > > >> > // ... > >> > } > >> > > >> > I have one goroutine to read the map and one goroutine to rewrite the > >> > global > >> > map variable does this safe ?? > >> > >> No. Use a lock. > >> > >> Ian > > > > -- > > 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...@googlegroups.com <javascript:>. > > 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. For more options, visit https://groups.google.com/d/optout.