Assume we have a type like follows

type Foo struct {
Alive bool

type Bar struct {
Foos []*Foo

In two goroutines we are supposed to read and write the Alive variable. For 

func (b *Bar) CheckFoosAlive()  {
for i := 0; i < len(b.Foos); i++ {
if b.Foos[i].Alive {

func (b* Bar) MarkFooStatus(index int, status bool){
// after bound checking
b.Foos[index].Alive = status

We would run both concurrently.

As we can clearly see putting a mutex for the entire Bar struct is seems a 
bit overhead for me. For example, if CheckFoosAlive is blocking and takes a 
lot of time, we don't want to lock until it's completed.

The whole idea is to reduce locking as much as possible to get access to an 
underlying element.

What is the best approach? (I replaced bools with uint32 and used atomic 
ops as for the current solution)
Would like to know your opinions


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 view this discussion on the web visit

Reply via email to