syncmap icon indicating copy to clipboard operation
syncmap copied to clipboard

is it deadlock?

Open mingpen opened this issue 9 years ago • 2 comments

var b *SyncMap func init() { b = New() for i := 0; i < 10000000 ; i++ { b.Set(strconv.Itoa(i),i) } } // go test -v -run=TestIter func TestIter(t *testing.T) { i := <- b.IterItems() t.Log(i) b.Set(i.Key,0) }

all goroutines are asleep - deadlock!

mingpen avatar May 12 '17 08:05 mingpen

should be iter func ?

mingpen avatar May 12 '17 08:05 mingpen

The IterItems and IterKeys methods are designed to iterate over items or keys to get data from the map. It is not encouraged to modify during loop, this maybe leads to deadlock.

An example usage:

for item := range b.Iteritems() {
    fmt.Println(item.Key, item.Value)
}

It's a good idea to add methods like ForEachKey and ForEachItem that accept a callback function to apply to each key or item. Pull requests are welcome.

DeanThompson avatar May 12 '17 10:05 DeanThompson