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 
example

func (b *Bar) CheckFoosAlive()  {
for i := 0; i < len(b.Foos); i++ {
if b.Foos[i].Alive {
fmt.Println("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

Regards,
Kasun

-- 
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.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/golang-nuts/5cfe5a3c-40a5-4cd6-a6ed-f5de2730b767%40googlegroups.com.

Reply via email to