Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-07 Thread Ian Lance Taylor
On Mon, Nov 7, 2016 at 12:57 AM, wrote: > All above discussion is talking about cpu excution order or memory order, > lets make it easy and clear. > If I use a mutex when create a temporary map to guarantee that this > temporary map has been successully created, modified and

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-07 Thread ayrb13
All above discussion is talking about cpu excution order or memory order, lets make it easy and clear. If I use a mutex when create a temporary map to guarantee that this temporary map has been successully created, modified and set to gMap1. Is it safe setting gMap1 pointer to gMap pointer when

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-07 Thread Asit Dhal
Hi, the second one is better. The lock should only protect the shared data. In this case, gMap. In the first case, you are protecting some computations which are not accessing any shared data. On November 7, 2016, at 4:03 AM, 刘桂祥 wrote:     should I use ??    

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-06 Thread Tamás Gulácsi
The second. The section in lock should be as short as possible. -- 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

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-06 Thread 刘桂祥
should I use ?? mu.Lock() temp := make(map[int]int) temp[1] = 100 temp[2] = 200 gMap = temp mu.Unlock() or temp := make(map[int]int) temp[1] = 100 temp[2] = 200 mu.Lock() gMap = temp mu.Unlock() 在 2016年11月4日星期五 UTC+8下午9:59:45,Ian Lance

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-04 Thread Michael Jones
to be more forgiving.) Michael From: <golang-nuts@googlegroups.com> on behalf of 刘桂祥 <liuguixiang...@gmail.com> Date: Friday, November 4, 2016 at 2:30 AM To: golang-nuts <golang-nuts@googlegroups.com> Cc: <liuguixiang...@gmail.com> Subject: Re: [go-nuts] Multiple-reader

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-04 Thread 刘桂祥
very thanks , I am unfamiliar with the cpu *out-of-order execution , but I doubt if my three line code is related ,it will reorder it ??* 在 2016年11月4日星期五 UTC+8下午1:38:12,Ian Lance Taylor写道: > > On Thu, Nov 3, 2016 at 10:28 PM, 刘桂祥 > wrote: > > > > In my write

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread Ian Lance Taylor
On Thu, Nov 3, 2016 at 10:28 PM, 刘桂祥 wrote: > > 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 You're right, my previous reply was incorrect. My

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread 刘桂祥
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, 刘桂祥 > wrote: > >

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread Ian Lance Taylor
On Thu, Nov 3, 2016 at 10:19 PM, 刘桂祥 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

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread 刘桂祥
can you explain why whis ? 在 2016年11月4日星期五 UTC+8下午1:16:39,Ian Lance Taylor写道: > > On Thu, Nov 3, 2016 at 8:37 PM, 刘桂祥 > wrote: > > // example.go > > > > package main > > > > var gMap = make(map[int]int) > > > > func w() { > > temp := make(map[int]int) > >

Re: [go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread Ian Lance Taylor
On Thu, Nov 3, 2016 at 8:37 PM, 刘桂祥 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,

[go-nuts] Multiple-reader single-writer map access is safe ?

2016-11-03 Thread 刘桂祥
// 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],