Hi there,

Just for heck of it - I am trying to come up with a ConcurrentCounter that 
does not suffer memory sharing or the cost of mutex lock/unlock.

The idea as described in the following Java code snippet is quite 
straightforward:

   - Pin every thread to a unique int32 variable.
   - Put these int32 variable in an array with sufficient 'dummy' elements 
   in between to eliminate false sharing.
   - In the inc() call, find the appropriate location using the thread/core 
   id and then increment it atomically.

https://github.com/ashkrit/blog/blob/master/src/main/java/counter/PaddedAtomicCounter.java

However, being new to Go, I am facing a few obstacles:

I thought of allocating a[numCores*16]uint32 and then use atomic.AddUint32() 
to atomically increment the individual element. However there are few basic 
things missing:

   - How do I get to know the Thread (or CpuCore) id on which the goroutine 
   is running? 
   - In the get() call of this counter, I need to sum up array values; how 
   does one ensure that the  latest values written to the array are visible to 
   the goroutine calling the get() method? Are the values written using 
   atomic package made visible to all cores immediately? Or else, does 
   atomic.LoadInt32()guarantee to return the latest values? I think the 
   problem is that atomic package does not say anything about visibility of 
   memory updates across cores.

--
cheers,
gaurav

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